How to call a function using apply with keyword args?

Cliff Wells logiplexsoftware at earthlink.net
Thu Jun 13 04:00:51 EDT 2002


On Thu, 2002-06-13 at 00:50, Martin v. Löwis wrote:
> "Achim Domma" <achim.domma at syynx.de> writes:
> 
> > I have a function like this:
> > 
> > def MyFkt(A,B,C): pass
> > 
> > and some parameters from a config file:
> > 
> > params = {'A':5,'B':8,'C':9}
> > 
> > how can I call MyFkt with this parameters? 
> 
> I recommend
> 
> MyFkt(**params)
> 
> > I tried apply(MyFkt,[],params) 
> 
> If you want to use apply, it is
> 
> apply(MyFkt, (), params)
> 
> The arguments need to be a tuple.

Actually, a list will work as well:

Python 2.1.1 (#1, Aug 13 2001, 19:37:40) 
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-96)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> def MyFkt(A,B,C): 
...     print A, B, C
... 
>>> params = {'A':5,'B':8,'C':9}
>>> apply(MyFkt,(),params)
5 8 9
>>> apply(MyFkt,[],params)
5 8 9
>>> 








More information about the Python-list mailing list