Newbie: How can I use a string value for a keyword argument?
bockman at virgilio.it
bockman at virgilio.it
Mon Feb 25 07:26:15 EST 2008
On 25 Feb, 12:42, Doug Morse <mo... at edoug.org> wrote:
> Hi,
>
> My apologies for troubling for what is probably an easy question... it's just
> that can't seem to find an answer to this anywhere (Googling, pydocs, etc.)...
>
> I have a class method, MyClass.foo(), that takes keyword arguments. For
> example, I can say:
>
> x = MyClass()
> x.foo(trials=32)
>
> Works just fine.
>
> What I need to be able to do is call foo() with a string value specifying the
> keyword (or both the keyword and value would be fine), something along the
> lines of:
>
> x = MyClass()
> y = 'trials=32'
> x.foo(y) # doesn't work
>
> or
>
> x.MyClass()
> y = 'trials'
> x.foo(y = 32) # does the "wrong" thing
>
Try this:
y='trials'
x.foo( **{y:32} )
Ciao
-----
FB
More information about the Python-list
mailing list