Having problems accepting parameters to a function

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue May 1 18:36:30 EDT 2007


En Tue, 01 May 2007 18:42:18 -0300, rh0dium <steven.klass at gmail.com>  
escribió:

> Let me expand a bit more - I am working on a threading class and I
> want to be able to push on the Queue a list of args.  If you run the
> following program - I am failing to understand how to push items onto
> the queue in a manner so that func2 recognizes them as kwargs not as
> args.  Can anyone help me with this.

You can put a tuple in the queue: the first item being the positional  
arguments, the second item being a dictionary used as keyword arguments.

py> from Queue import Queue
py> q = Queue()
py> q.put(((1,2,3),{"a":100, "b":200}))
py> q.put(((),dict(boca=2,racing=2)))
py> def f(*args, **kw):
...   print "args", args
...   for k in kw:
...     print k, kw[k]
...
py> item = q.get()
py> f(*item[0], **item[1])
args (1, 2, 3)
a 100
b 200
py> args, kw = q.get()
py> f(*args, **kw)
args ()
racing 2
boca 2

-- 
Gabriel Genellina



More information about the Python-list mailing list