Difference between default arguments and keyword arguments

DoubleM DoublemPI at netscape.net
Sun Apr 4 09:25:19 EDT 2004


Edward Diener wrote:
> In the tutorial on functions there are sections on default arguments and
> keyword arguments, yet I don't see the syntactic difference between them.
> For default arguments the tutorial shows:
> 
> def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
> 
> while for keyword arguments the tutorial shows:
> 
> def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
> 
> The syntax 'keyword = value' is used for both as far as I can see. How does
> one distinguish between them or are they both part of the same combined
> concept, which is: if one calls the function with less than the required
> number of arguments but does specify keyword values, those values are used,
> else the defaults are supplied. Or is there really a syntactic difference
> between default arguments and keyword arguments which I have missed above ?
> 
> 
All arguments are keyword arguments.  They may or may not have a default 
value.  In your example, voltage is a keyword argument, but it has no 
default.

Consider the following:
 >>> def fn(a,b):
	print 'a = ',a
	print 'b = ',b

	
 >>> fn(b=1,a=2)
a =  2
b =  1
 >>> fn(1,2)
a =  1
b =  2
 >>>
Hope this helps,
Mike



More information about the Python-list mailing list