Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > syntax error in eval()

Reply
Thread Tools

syntax error in eval()

 
 
harold fellermann
Guest
Posts: n/a
 
      01-10-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

 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      01-10-2005
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})

> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "<string>", line 1
> X.attr = val
> ^
> SyntaxError: invalid syntax


You may want to use exec instead of eval:

py> class X(object):
.... pass
....
py> attrname = "attr"
py> exec "X.%s = val" % attrname in dict(X=X, val=5)
py> X.attr
5

But personally, I'd use setattr, since that's what it's for:

py> class X(object):
.... pass
....
py> attrname = "attr"
py> setattr(X, attrname, 5)
py> X.attr
5

Steve
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      01-10-2005
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})

> 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.


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 because eval evaluates an expression and
an assignment statement is a statement not an expression.
 
Reply With Quote
 
harold fellermann
Guest
Posts: n/a
 
      01-10-2005
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 because eval evaluates an expression and
> an assignment statement is a statement not an expression.


--
Abandon the search for Truth -- settle for a good fantasy.
--

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax error? What syntax error? Assignment fo default values? Mark Richards Perl Misc 3 11-18-2007 05:01 PM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
[ANN] SqlStatement 1.0.0 - hide the syntax of SQL behind familiarruby syntax Ken Bloom Ruby 3 10-09-2006 06:46 PM
Syntax highligth with textile: Syntax+RedCloth ? gabriele renzi Ruby 2 12-31-2005 02:44 AM
Complier error!! error C2059: syntax error : '(' Balaji C++ 3 12-03-2004 11:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57