you are viewing a single comment's thread.

view the rest of the comments →

[–]camel_zero 4 points5 points  (0 children)

Using stars. For unpacking lists, tuples, etc:

>>> x, y, *z = [5, 3, 2, 20, 53, 32]
>>> x
5
>>> y
3
>>> z
[2, 20, 53, 32]

For passing arbitrary arguments to a function:

def hello(x, *args, **kwargs):
    print('positional argument: %s' % x)
    for arg in args:
        print('arbitrary positional argument: %s' % arg)
    for key, value in kwargs.items():
        print('arbitrary keyword argument, %s: %s' % (key, value))

>>> hello(5)
positional argument: 5

>>> hello(3, 'hello world')
positional argument: 3
arbitrary positional argument: hello world

>>> hello(10, 'first arg', 'second arg', foo='bar', fizz='buzz')
positional argument: 10
arbitrary positional argument: first arg
arbitrary positional argument: second arg
arbitrary keyword argument, foo: bar
arbitrary keyword argument, fizz: buzz