Difference between default arguments and keyword arguments

John Roth newsgroups at jhrothjr.com
Sun Apr 4 18:41:16 EDT 2004


"Edward Diener" <eldiener at earthlink.net> wrote in message
news:UmTbc.9904$NL4.4809 at newsread3.news.atl.earthlink.net...
> 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
?

The difference (from the tutorial) is that default arguements apply to
the function *definition*, while keyword arguements apply to the
function *call*. It's a subtle difference that does seem to be quite
easy to miss.

In going through the tutorial I also noticed that it explained
the * notation, but not the ** notation. I think this also needs
to be repaired - if one is in the tutorial, both should be.

John Roth





More information about the Python-list mailing list