syntax error in eval()
harold fellermann
harold.fellermann at upf.edu
Mon Jan 10 11:45:28 EST 2005
Hi all,
I am trying to dynamically add class attributes at runtime using the
function eval(),
i.e. I want to do something like
>>> class X : pass
...
>>> X.attr = 5
but without knowing either the attributes name nor its value.
However, I encounter a syntax error I cannot understand:
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})
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 1
X.attr = val
^
SyntaxError: invalid syntax
Does anyone have a clue what might be wrong? Thanks in advance.
- harold -
--
"I know what I believe. I will continue to articulate what I believe
and what I believe - I believe what I believe is right."
-- George W. Bushman
More information about the Python-list
mailing list