[Tutor] Dictionaries supplying arguments to functions

Kalle Svensson kalle@gnupung.net
Tue, 7 Aug 2001 04:41:56 +0200


Sez Tom Nott:
> Is it possible to have a dictionary supply the arguments to a function?
> 
> I'm after something like:
> 
> def fn(p1, p2, p3):
>     print p1
>     print p2
>     print p3
> 
> d = {'p1':'How ', 'p2':'are ', 'p3':'you'}
> 
> callWithDict(fn, d)

Yes.
In all (modern, at least) Python versions:

apply(fn, (), d)

In Python 2.0 and later:

fn(**d)

does the same thing.  apply() still works, but looks a little ugly.

More about apply():
http://www.python.org/doc/current/lib/built-in-funcs.html

Peace,
  Kalle
-- 
Free Dmitry Sklyarov! - http://www.freesklyarov.org/