Function that knows the names of its actual parameters
John W. Baxter
jwbnews at scandaroon.com
Fri Jun 16 16:20:59 EDT 2000
In article <394a6395.7217524 at news.btx.dtag.de>, spamfranke at bigfoot.de
(Stefan Franke) wrote:
> I found myself in need for the following functionality a couple of times:
>
> >>> a=1
> >>> b="foo"
> >>> c=b
> >>> magic(c, b, a)
> {'a': 1, 'b': 'foo', 'c': 'foo'}
>
> that is, a function that goes one step further than **dict arguments and
> returns
> a dictionary with names and values of its actual parameters.
> It's useful every time one tries to write generators for Python or other
> code (right
> now I'm writing some SQL code emitting functions).
I would think that this method should point the way (you can have some
positional arguments as well, along with named keyword ones, plus the
catchall):
>>> def m(**args):
... print args
...
>>> m(a=5, b='ten')
{'b': 'ten', 'a': 5}
One example is on page 109 of "Learning Python", with discussion
preceeding and following.
--John
--
John W. Baxter Port Ludlow, WA USA jwbnews at scandaroon.com
More information about the Python-list
mailing list