[Tutor] calling a function with a variable number of arguments

A.M. Kuchling amk at amk.ca
Wed Nov 12 07:34:40 EST 2003

On Wed, Nov 12, 2003 at 12:32:06PM +0100, Eur van Andel wrote:
> Am I correct in assuming that a function:
> def funct_1(a0, a1=1, a2=2)
> can be called with either 1, 2 or 3 arguments?


> And if I call it:
> funct_1(4, 4)
> a2 will stay 2?
> And so to modify the default value for a2, three arguments are needed?

Also correct.  

If you specify keyword arguments, you can supply them in any order, e.g.
funct_1(a1=4, a2=3, a0=1).  You couldn't leave off the 'a0' value because it
has no specified default; you'd get a TypeError exception with the message
'funct_1() takes at least 1 non-keyword argument (0 given)'.

http://www.python.org/doc/current/ref/calls.html explains this very formally
-- so formally that it might be confusing.  Anyone know of a better
explanation of Python function calls?


More information about the Tutor mailing list