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

Discussion in 'Python' started by Doug Morse, Feb 25, 2008.

  1. Doug Morse

    Doug Morse Guest

    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.

    Thanks in advance for any and all assistance!

    Doug
    Doug Morse, Feb 25, 2008
    #1
    1. Advertising

  2. Doug Morse

    John Machin Guest

    On Feb 25, 10:42 pm, Doug Morse <> 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
    John Machin, Feb 25, 2008
    #2
    1. Advertising

  3. Doug Morse

    Guest

    On 25 Feb, 12:42, Doug Morse <> 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
    , Feb 25, 2008
    #3
  4. Doug Morse a écrit :
    > 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


    You want something like:

    x.foo(**{'trials':32})


    > Just for completeness, my goal is simply to read a bunch of key/value pairs
    > from an INI file (using ConfigObj)


    ConfigObj being a subclass of dict, you should be able to use it
    directly, ie:

    x.foo(**my_config)

    If just want to pass a section of the ConfigObj, it should work just the
    same.

    HTH
    Bruno Desthuilliers, Feb 25, 2008
    #4
  5. Doug Morse

    Doug Morse Guest

    On Mon, 25 Feb 2008 04:20:37 -0800 (PST), John Machin <>
    wrote:
    > On Feb 25, 10:42 pm, Doug Morse <> 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


    Hi John,

    Your response is most helpful and informative -- thanks!

    I don't think that setattr() is exactly what I need, though, as foo() doesn't
    actually create or update its instance attributes. What I need to be able to
    do is call foo() specifying keyword arguments not directly but viz a viz
    another variable or variables that contain the keywords and values.

    I'm pretty sure I just found the solution, which is to use the **-operator on
    a dictionary. Actually, ConfigObj (the INI file reader) subclasses from
    __builtin__.dict (i.e., the class/object *is* a dictionary), so the following
    seems to work perfectly:

    x.foo(**config)

    This sends ALL the key/value pairs in config as keyword/value pairs to foo(),
    which is exactly what I need.

    Just FYI, I located this solution via Google shortly after posting, so I have
    sent a cancel request on my original post.

    Thanks again!
    Doug
    Doug Morse, Feb 25, 2008
    #5
  6. Doug Morse

    Gary Herron Guest

    Doug Morse 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
    >

    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:

    kw = {somestring:32}
    x.foo(**kw)

    Gary Herron

    > 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.
    >
    > Thanks in advance for any and all assistance!
    >
    > Doug
    >
    >
    >
    >
    >
    >
    Gary Herron, Feb 25, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. LaundroMat
    Replies:
    50
    Views:
    947
    Hendrik van Rooyen
    Oct 14, 2006
  2. Replies:
    6
    Views:
    442
    Peter Otten
    May 10, 2007
  3. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    343
  4. ±è ÁØ¿µ
    Replies:
    7
    Views:
    238
    Einar Magnús Boson
    Nov 27, 2008
  5. Replies:
    5
    Views:
    121
Loading...

Share This Page