f(n=4) works; bug or feature?

John W. Baxter jwbaxter at spamcop.net
Mon Dec 24 01:22:36 EST 2001


In article <mailman.1009170148.18924.python-list at python.org>, Sean
'Shaleh' Perry <shalehperry at attbi.com> wrote:

> On 24-Dec-2001 Edward C. Jones wrote:
> > In Python 2.2, the following code prints a "4".
> > 
> > def fun(n):
> >      print n
> > 
> > fun(n=4)
> > 
> > Bug or feature?
> > 
> 
> feature.  It is called a keyword argument.
> 
> def func(a, b, c):
>         print a
>         print b
>         print c
> 
> func(b = 1, a = 8, c = 'hello')
> 
> The idea here is you can call the function with the arguments in any order as
> long as you know their names.  Hnady for long function calls like gui toolkits
> or network code.

And handy for functions with lots of optional arguments where you're
happy with the defaults except for one or two somewhere near the "right
end" of the argument list.

  --John



More information about the Python-list mailing list