How to call a function using apply with keyword args?

Cliff Wells logiplexsoftware at earthlink.net
Thu Jun 13 03:52:27 EDT 2002


On Thu, 2002-06-13 at 00:09, Achim Domma wrote:
> Hi,
> 
> 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 tried apply(MyFkt,[],params)
> but get this error:
> 
> TypeError: MyFkt() takes exactly 3 non-keyword arguments (0 given)

Works for me:

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
>>> 

You can also use:

>>> MyFkt(**params)
5 8 9
>>> 

Regards,
Cliff






More information about the Python-list mailing list