Re: Newbie: How can I use a string value for a keyword argument?



On Feb 25, 10:42 pm, Doug Morse <mo...@xxxxxxxxx> 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

Surely there's some way to use a string's value as the key for making a method
call with a keyword argument?

Just for completeness, my goal is simply to read a bunch of key/value pairs
from an INI file (using ConfigObj) and then use those key/value pairs to set a
(3rd party) object's parameters, which must be done with a call along the
lines of "instance.set(key=value)". Obviously, I could create a huge if..elif
statement along the lines of "if y = 'trials': x.foo(trials=32); elif y =
'speed': x.foo(speed=12);" etc., but then the statement has to be maintained
every time a new parameter is added/changed etc. Plus, such a solution seems
to me grossly inelegant and un-Pythonic.


I'm not quite sure what foo() is really supposed to do ... however the
built-in function setattr is your friend. Assuming that ini_dict
contains what you have scraped out of your .ini file, you can do:
x = MyCLass()
for key, value in ini_dict.items():
setattr(x, key, value)
You may prefer (I would) to do it inside the class, and maybe do some
checking/validation:
class MyClass(object):
def load(self, adict):
for k, v in adict.items():
# do checking here
setattr(self, k, v)
# much later
x = MyClass()
x.load(ini_dict)

HTH,
John
.



Relevant Pages

  • Re: C# confusion
    ... If you create instances of MyClass, ... you are allocating an area of memory for the data segment for each ... data members are initialised upon first reference of the class. ... static keyword; This one has been hitting me like a hammer on the ...
    (microsoft.public.dotnet.languages.csharp)
  • Newbie: How can I use a string value for a keyword argument?
    ... I have a class method, MyClass.foo, that takes keyword arguments. ... x = MyClass() ... my goal is simply to read a bunch of key/value pairs ...
    (comp.lang.python)
  • Re: Newbie: How can I use a string value for a keyword argument?
    ... I have a class method, MyClass.foo, that takes keyword arguments. ... x = MyClass() ... my goal is simply to read a bunch of key/value pairs ... I'm not quite sure what foo() is really supposed to do ... ...
    (comp.lang.python)
  • Re: using of "@" before classname
    ... public class @MyClass ... can anyone please help me to know that when and why we use this keyword ... It allows reserved words to be used as identifiers. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Newbie: How can I use a string value for a keyword argument?
    ... I have a class method, MyClass.foo, that takes keyword arguments. ... x = MyClass() ... Keyword args are represented during the calling process as a dictionary, You can create the dictionary yourself, and slip it into the calling arguments with a ** notation: ... my goal is simply to read a bunch of key/value pairs ...
    (comp.lang.python)