Reputation: 53
I have a python 2.5 function with a definition like:
def Foo(a, b='Silly', c='Walks', d='Spam', e='Eggs', f='Ni', *addl):
Where addl
can be any number of strings (filenames) to do something with.
I'm fine with all of the defaults, but I have filenames to put in addl
.
I am inclined to do something like:
Foo('[email protected]', addl=('File1.txt', 'File2.txt'))
But that gets the following error:
TypeError: Foo() got an unexpected keyword argument 'addl'
Is there a syntax where I can succinctly call Foo
with just the first required parameter and my (variable number of) additional strings? Or am I stuck redundantly specifying all of the defaults before I can break into the addl
argument range?
For the sake of argument, the function definition is not able to be modified or refactored.
Upvotes: 3
Views: 88
Reputation: 1499
Ok, if function can't be modified, then why not create wrapper?
def foo_with_defaults(a, *addl):
Foo(a, *(Foo.func_defaults + addl))
foo_with_defaults('[email protected]', *('File1.txt', 'File2.txt'))
Upvotes: 2
Reputation: 1043
You are trying to use addl
as a keyword argument, but you defined it as a positional argument. You should add one more *
if you want to call the function like Foo(addl=('File1.txt', 'File2.txt'))
:
def Foo(a, b='Silly', c='Walks', d='Spam', e='Eggs', f='Ni', **addl):
Upvotes: 0
Reputation: 390
To my knowledge it is not possible.
First you need to pass required (a) and default (b, c, d, e, f) arguments, and then every additional argument is passed as *addl:
Foo('[email protected]', 'Silly', 'Walks', 'Spam', 'Eggs', 'Ni', 'File1.txt', 'File2.txt')
Upvotes: 0