Re: use str as variable name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fredrik Lundh

    Re: use str as variable name

    Mathieu Prevot wrote:
    I have a program that take a word as argument, and I would like to
    link this word to a class variable.
    >
    eg.
    class foo():
    width = 10
    height = 20
    >
    a=foo()
    arg='height'
    a.__argname__= new_value
    >
    rather than :
    >
    if arg == 'height':
    a.height = new_value
    elif arg == 'width';
    a.width = new_value
    >
    Can I do this with python ? How ?
    assuming you mean "instance variable" ("a" is an instance of the class
    "foo"), you can use setattr:

    a = foo()
    arg = 'height'
    setattr(a, arg, new_value)

    </F>

Working...