Thank you, Duncan and Steven.
I completely forgot about setattr.
Of course that's the way ... as its name might suggest *g*
What you are doing wrong is attempting to use eval before exhausting
all
the simpler techniques. Why not just call 'setattr'?
setattr(X, 'attr', 5)
BTW, the syntax error is
harold fellermann wrote:
> Python 2.4 (#1, Dec 30 2004, 08:00:10)
> [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> class X : pass
> ...
> >>> attrname = "attr"
> >>> eval("X.%s = val" % attrname , {"X":X
harold fellermann wrote:
Python 2.4 (#1, Dec 30 2004, 08:00:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class X : pass
...
>>> attrname = "attr"
>>> eval("X.%s = val" % attrname , {"X":X, "val":5})