Is there a more succint way to write this?
f(a=a, b=b, c=c, d=d, e=e)
Background: I have a function with too many arguments
f(a, b, c, d, e):
pass
I my program I have local variables that are named exactly same as the function parameters.
a, b, c, d, e = range(5)
I would like to call the function with keyword arguments. Since the variables are named the same, this is how the call would look.
g = f(a=a, b=b, c=c, d=d, e=e) # this can get very long
Of course, I can pass the aruguments using position instead of keywords like this
g = f(a, b, c, d, e)
But a
, b
, c
, d
, e
are just the names of variables in this example and it is easy to see the correct order. However unfortunately the variables in my program are named more complicatedly and there is no easily discernible natural order. So I really like to pass them by keyword to avoid any mistakes.
Answer. When a parameter has the same name as a variable defined outside, instead of the function using the variable defined outside, it will only reference the value that was passed to the parameter. So, parameters will be used over variables of the same name within a function.
This name is used within the method body to refer to the passed-in argument. The name of a parameter must be unique in its scope. It cannot be the same as the name of another parameter for the same method or constructor, and it cannot be the name of a local variable within the method or constructor.
Bottom line: you can't have two things simultaneously with the same name, be it a function, an integer, or any other object in Python. Just use a different name.
We cannot use a keyword as a variable name, function name or any other identifier. Keywords are reserved words that have a special...
You could do something like the following:
a, b, c, d, e = range(5)
arg_dict = lambda l: dict((k, globals()[k]) for k in l.split(', '))
arg_dict('a, b, c, d, e') => {'a': 0, 'c': 2, 'b': 1, 'e': 4, 'd': 3}
, so you can call your function like this:
f(**arg_dict('a, b, c, d, e'))
This gives you the ability to specify exactly which variables you want to use. An alternative method for this that does not use globals()
would be to use eval()
, but it could make the use of the lambda potentially unsafe.
arg_dict = lambda l: dict(zip(l.split(', '), eval(l)))
If you would prefer to pass locals()
in as an argument instead of using globals()
in the lambda you can use the following:
arg_dict = lambda l, d=locals(): dict((k, d[k]) for k in l.split(', '))
f(**arg_dict('a, b, c, d, e'))
Thanks to senderle for the locals()
suggestions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With