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
1 Answer 1
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.
-
1thats 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?Code Review Doctor– Code Review Doctor2013年01月15日 16:38:51 +00:00Commented Jan 15, 2013 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– Jimmy Hoffa2013年01月15日 17:12:28 +00:00Commented Jan 15, 2013 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– Martijn Pieters2013年01月15日 17:16:43 +00:00Commented Jan 15, 2013 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?Code Review Doctor– Code Review Doctor2013年01月15日 17:34:33 +00:00Commented Jan 15, 2013 at 17:34
-
1@rikAtee: No, not really. You'd use a module instead.Martijn Pieters– Martijn Pieters2013年01月15日 17:36:04 +00:00Commented Jan 15, 2013 at 17:36
action
is taken from user input for example); I believe the OP issue is with the proliferation of functions to define.