The def construct is used to define
functions and methods. Here is the general form:
defn(p0[=e0][,p1[=e1]]...[,*pv][,**pd]):B
The name of
the function is followed by a pair of parentheses
containing descriptions of the arguments to the function.
The block n is
called the body of the function,
and is executed when the function is called.
B
A function may have no arguments at all. If there are arguments to be passed to the function when it is called, they must be declared in this order:
A positional argument is a
name that is not followed by an equal sign (=) and default value.
A keyword argument is followed by an equal sign and an expression that gives its default value.
If a function has both positional arguments and keyword arguments, all positional arguments must precede all keyword arguments.
If there is a *
parameter, when the function is called that name is
bound to a (possibly empty) tuple of all positional
arguments passed to the function that do not correspond
to other positional or keyword arguments in the pvdef.
If there is a **
parameter, when the function is called that name is
bound to a dictionary of all keyword arguments
passed to the function that do not appear in the
function's pddef.
When you call a function, the argument values you pass to it must obey these rules:
There are two kinds of arguments: positional (also called non-default arguments) and keyword (also called default arguments). A positional argument is simply an expression, whose value is passed to the argument.
A keyword argument has this form:
name=expression
All positional arguments in the function call (if any) must precede all keyword arguments (if any).
>>> def wrong(f=1, g): ... print f, g ... File "<stdin>", line 1 SyntaxError: non-default argument follows default argument
You must supply at least as many positional arguments as the function expects.
>>> def wantThree(a, b, c):
... print a,b,c
...
>>> wantThree('nudge', 'nudge', 'nudge')
nudge nudge nudge
>>> wantThree('nudge')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: wantThree() takes exactly 3 arguments (1 given)
If you supply more positional arguments than the
function expects, the extra arguments are matched
against keyword arguments in the order of their
declaration in the def. Any additional
keyword arguments are set to their default values.
>>> def f(a, b, c=1, d='elk'): ... print a,b,c,d ... >>> f(99, 111) 99 111 1 elk >>> f(99, 111, 222, 333) 99 111 222 333 >>> f(8, 9, 10, 11, 12, 13) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f() takes at most 4 arguments (6 given)
You may supply arguments for keyword parameters in any
order by using the form , where k=v
is the keyword used in the declaration of that
parameter and k is your desired argument.
v
>>> def blackKeys(fish='Eric', dawn='Stafford', attila='Abdul'): ... print fish, dawn, attila ... >>> blackKeys() Eric Stafford Abdul >>> blackKeys(attila='Gamera', fish='Abdul') Abdul Stafford Gamera
If you declare a parameter of the form “*”,
the caller can provide any number of additional
keyword arguments, and the name will be bound to a tuple
containing those additional arguments.
name
>>> def posish(i, j, k, *extras): ... print i,j,k,extras ... >>> posish(38, 40, 42) 38 40 42 () >>> posish(44, 46, 48, 51, 57, 88) 44 46 48 (51, 57, 88)
Similarly, you may declare a final parameter of the
form “**”. If the caller provides any keyword
arguments whose names do not match declared keyword
arguments, that name will be bound to a dictionary containing
the additional keyword arguments as key-value pairs.
name
>>> def extraKeys(a, b=1, *c, **d):
... print a, b, c, d
...
>>> extraKeys(1,2)
1 2 () {}
>>> extraKeys(3,4,6,12, hovercraft='eels', record='scratched')
3 4 (6, 12) {'record': 'scratched', 'hovercraft': 'eels'}