Having problems accepting parameters to a function

Stargaming stargaming at gmail.com
Tue May 1 12:47:34 EDT 2007


rh0dium schrieb:
> Hi Experts!!
> 
> I am trying to get the following little snippet to push my data to the
> function func().  What I would expect to happen is it to print out the
> contents of a and loglevel.  But it's not working.  Can someone please
> help me out.
> 
> ---------<snip>--------------
> #!/usr/bin/env python
> 
> import random
> 
> def func(*args, **kwargs):
>    print kwargs.get('a', "NOPE")
>    print kwargs.get('loglevel', "NO WAY")
> 
> def main():
>    b = []
>    for x in range(5):
>       b.append({'a':random.random(), "loglevel":10})
> 
>    for y in b:
>       apply(func,y)
> 
>     # First attempt - didn't work
>     # for y in b:
>     #   func(y)
> 
> if __name__ == '__main__':
>    main()
> 
``apply()`` is deprecated -- use the asterisk-syntax_ instead.

 >>>> dic = {'a': 5, 'loglevel': 10}
 >>> def foo(*args, **kwargs):
...   print kwargs
...
 >>> foo(**dic)
{'a': 5, 'loglevel': 10}

So, your first attempt was close -- just two signs missing. :-)

HTH,
Stargaming

.. _asterisk-sytax: 
http://docs.python.org/tut/node6.html#SECTION006740000000000000000



More information about the Python-list mailing list