[ 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]

Unpacking argument lists

In a function call:

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

limits = [3,6]

**kwargs: unpacks a dictionary for keyword arguments.

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