Sunday 4 July 2010

Passing arguments to Python functions

I found a good discussion on how to pass vectors and dictionaries as arguments to Python functions. It can be found here.

In summary, for unnamed arguments:

>>> a = (1,4)
>>> range( *a )
[1, 2, 3]


And for named arguments:

>>> def concat(a,b): return a + b
...
>>> a = [1,2,3]
>>> b = range( 4,7)
>>> b
[4, 5, 6]
>>> concat(**dict( a = a, b = b ) )
[1, 2, 3, 4, 5, 6]


Named and unnamed arguments can be mixed:

my_foo( *a, **b )

There is only one caveat with functions requiring a single, unnamed argument. See the link above for details.

No comments:

Post a Comment