Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a function containing other functions. These function are called based on value of variable action.

These sub functions are logically grouped - i.e., they all deal with file manipulation.

Each function has 2 to 5 lines.

I would like to improve the style.

Is there a more concise way of achieving this end than what I have below? I feed there is a log of defining functions that contain not many LOC.

def my_function(action):

    return { '1': func_a, '2': func_b, '3': func_c, '4': func_d}.get(action, err)()

    def func_a():
        ...
        ...
        return ...
    def func_b():
        ...
        ...
        ...
        return ...
    def func_c():
        ...
        ...
        return ...
    def func_d():
        ...
        ...
        return ...
    def err():
        return

*discaimer: actual functions have logical names.

My issue is with the proliferation of functions to define

share|improve this question
    
You cannot define your map before your functions like that, but we get the example. –  Martijn Pieters Jan 15 '13 at 15:58
    
Is there any functionality shared by the functions that you can factor out? Otherwise, you'll still have to define the functions.. –  Martijn Pieters Jan 15 '13 at 16:05
3  
@MichaelT: I don't think that is the issue here; using a dict to manage the dispatch is also safer (if action is taken from user input for example); I believe the OP issue is with the proliferation of functions to define. –  Martijn Pieters Jan 15 '13 at 16:14
    
@MartijnPieters is correct –  rikAtee Jan 15 '13 at 16:19
    
To the 'off topic' voters: note that this question was already voted off-topic on Stack Overflow with advice to come ask here. This is on-topic here; it's a design pattern question, not a specific coding problem. –  Martijn Pieters Jan 16 '13 at 11:23

1 Answer 1

up vote 2 down vote accepted

If you cannot factor out common code in these functions they may not be reduced in size, if you cannot factor out features into common feature paths they cannot be reduced in number.

That said this approach is marvelous in my opinion and I have used dictionaries of functions to great effect in the past.

I can give one suggestion to your approach: Don't nest all the functions inside of the dispatching function. This may alleviate some of your concerns of cleanliness, perhaps something like:

def my_function(action):
    return { '1': func_a, '2': func_b, '3': func_c, '4': func_d}.get(action, err)()

def func_a():
    ...
    ...
    return ...
def func_b():
    ...
    ...
    ...
    return ...

By having separate functions which return the dispatchable functions, you can effectively move those functions to wherever they best fit in your system hierarchy, and your dispatcher simply calls them out. However if the nesting approach is the best modeling given your system then stick with what you have.

share|improve this answer
1  
thats great, thanks. One q: why do you nest a function inside each of the called functions then return the nested function? Is it so the dict contains e.g., func_a() instead of func_a and so the reader can easily see this is a function? –  rikAtee Jan 15 '13 at 16:38
    
@rikAtee I do not know the rules of referencing a function in python, if you can directly reference func_a when they're not nested then forget about that extra layer of abstraction, in some other languages the only way to reference a function is to call a function that returns a function or create the function in the current scope (like you were doing). I presumed you were nesting them because you were required to follow the same rules, I'm guessing python doesn't force those rules then from your comment and I'll fix my example accordingly. –  Jimmy Hoffa Jan 15 '13 at 17:12
    
@rikAtee: Only use nested functions when you need to nest the scope; if the nested function uses a variable defined in the parent function. –  Martijn Pieters Jan 15 '13 at 17:16
    
@MartijnPieters what about when the sub functions are a 'family' of related functions but live in a file that is populated by many families? I though it logical to group the 'related' functions as nested within a parent function. Is this sensible? –  rikAtee Jan 15 '13 at 17:34
1  
@rikAtee: No, not really. You'd use a module instead. –  Martijn Pieters Jan 15 '13 at 17:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.