indirect **keyword calls

Sean 'Shaleh' Perry shalehperry at attbi.com
Mon Oct 28 04:13:27 EST 2002


On Monday 28 October 2002 00:59, Terry Hancock wrote:
> Hi all,
> I've just recently come across a need for this idiom (isn't
> this new in Python 2.x?):
>
> def my_func(spam, ham=1, **eggs):
>      #...
>      pass
>
> where 'eggs' gets a dictionary of keyword arguments.  I
> haven't previously had any experience with it.
>
> It seems that Zope's ZSQL methods accept their arguments
> in this form, but I need to provide an intermediate layer.
> Is there any way of writing a general call that passes a
> received set of keywords on to a called function.

From the interpreter:

>>>> def my_real(**kw):
...   print kw
... 
>>> my_real(this=1, that=2)
{'this': 1, 'that': 2}
>>> def my_wrap(**kw):
...   my_real(**kw)
... 
>>> my_wrap()
{}
>>> my_wrap(this=1, that=2)
{'this': 1, 'that': 2}

The point is you pass the dictionary with the keywords onto the next function 
by continuing to use the **kw syntax.  BTW kw is just a convention you can 
call it whatever you want.




More information about the Python-list mailing list