File: class/Workbook/Exercises/Lab4/ex8.txt

def f1(a, b): print a, b             # normal args

def f2(a, *b): print a, b            # positional varargs

def f3(a, **b): print a, b           # keyword varargs

def f4(a, *b, **c): print a, b, c    # mixed modes

def f5(a, b=2, c=3): print a, b, c   # defaults

def f6(a, b=2, *c): print a, b, c    # defaults + positional varargs


% python
>>> f1(1, 2)                  # matched by position (order matters)
1 2
>>> f1(b=2, a=1)              # matched by name (order doesn't matter)
1 2

>>> f2(1, 2, 3)               # extra positionals collected in a tuple
1 (2, 3)

>>> f3(1, x=2, y=3)           # extra keywords collected in a dictionary
1 {'x': 2, 'y': 3}

>>> f4(1, 2, 3, x=2, y=3)     # extra of both kinds
1 (2, 3) {'x': 2, 'y': 3}

>>> f5(1)                     # both defaults kick in
1 2 3
>>> f5(1, 4)                  # only one default used
1 4 3

>>> f6(1)                     # one argument: matches "a"
1 2 ()
>>> f6(1, 3, 4)               # extra positional collected
1 3 (4,)



[Home page] Books Code Blog Python Author Train Find ©M.Lutz