Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythonic way to pass around many arguments

I'm working on a package that contains subpackages and several modules within each subpackage. Most of the functions need several arguments (~10) that are initiated in the "main" function. Usually, all the arguments are passed to methods that are called from within a function. What is the best way to pass around these parameters ?

Consider the following scenario:

def func(arg1, arg2, ...., argn):
  do something  
  ext_method(arg1, arg3,...argk) # Note: all the arguments are not passed to ext_method
  cleanup  

PS: Function nesting can go quite deep (say around 10-12 functions starting from main function). I considered two alternatives:

  1. Inspect method: Construct a dict of all the arguments in the main function with all the arguments. To call a function: Get its argspec using the method in inspect package, and filter out unnecessary arguments from the "global" arguments dict. It looks something like the below snippet.

    args = {arg1: obj1, arg2:obj2,...}  
    req_args = dict([(k, v) for k, v in args.items() if k in inspect.getargspec(ext_method).args])
    
  2. key-value method: ext_method() is called using argument key-value pairs

    ext_method(arg1=val1, arg3=val3,...,argk=valk)
    

I find the second method to be ineffective because its not easy to change the signature of ext_method() as more features are added to the package. Is method 1 a better alternative ?

like image 426
Graddy Avatar asked Feb 03 '14 12:02

Graddy


People also ask

How do you pass multiple arguments to a function?

The * symbol is used to pass a variable number of arguments to a function. Typically, this syntax is used to avoid the code failing when we don't know how many arguments will be sent to the function.

How do I pass multiple arguments in CMD?

It's basically the same as how you'd pass them in command prompt: Separate the arguments by space, Enclose each argument with double quotes, " if metacharacters are involved.

How do you pass multiple arguments in Java?

Parameters and Arguments Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

How do you pass multiple keyword arguments in Python?

**kwargs: Pass multiple arguments to a function in Python If so, use **kwargs . **kwargs allow you to pass multiple arguments to a function using a dictionary. In the example below, passing **{'a':1, 'b':2} to the function is similar to passing a=1, b=1 to the function.


1 Answers

More, better options:

  1. Add **kwargs to your functions and ignore extra arguments:

    ex_method(arg1, arg2, **kw)
    
  2. Pass around an object with all arguments as attributes; a namedtuple class would be ideal here. Each function only uses those attributes it needs.

I'd pick the latter. Stop moving around 10 arguments, start passing around just one.

Even better, if all those operations are really coupled to the data those arguments represent, perhaps you should be using a class instead:

class SomeConcept(object):
    def __init__(self, arg1, arg2, arg3, .., argN):
        self.arg1 = arg1
        # ... etc.

    def operation1(self, external_argument):
        # use `self` attributes, return something.

instead of all those separate functions.

like image 171
Martijn Pieters Avatar answered Nov 05 '22 17:11

Martijn Pieters