[ Prev ] [ Next ] | Open Codex Zim wiki by Joseph Reagle.

Python Tips

*, ** (splat)


Arbitrary argument lists


In a function definition * provides an object with an arbitrary number of arguments; it must be after zero or more normal arguments.

def double(*args):
return [a*2 for a in args]
double(1,2,8)


Unpacking argument lists


In a function call:

*args: unpacks items from a list or tuple when provided as an argument.

limits = [3,6]
range(*limits)


**kwargs: unpacks a dictionary for keyword arguments.



© 2014-2016 Joseph Reagle. Please reuse and share! Creative Commons License