syntax error in eval()

Discussion in 'Python' started by harold fellermann, Jan 10, 2005.

  1. 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
     
    harold fellermann, Jan 10, 2005
    #1
    1. Advertising

  2. 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
     
    Steven Bethard, Jan 10, 2005
    #2
    1. Advertising

  3. harold fellermann

    Duncan Booth Guest

    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.
     
    Duncan Booth, Jan 10, 2005
    #3
  4. 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.
    --
     
    harold fellermann, Jan 10, 2005
    #4
    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. Eric Newton
    Replies:
    3
    Views:
    9,486
    Brock Allen
    Apr 4, 2005
  2. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    563
    Karl Seguin [MVP]
    Jun 16, 2006
  3. Alex van der Spek

    eval('07') works, eval('08') fails, why?

    Alex van der Spek, Jan 8, 2009, in forum: Python
    Replies:
    6
    Views:
    1,487
    Bruno Desthuilliers
    Jan 8, 2009
  4. Liang Wang
    Replies:
    8
    Views:
    141
    Ben Morrow
    Feb 2, 2008
  5. Marc Girod

    to eval or not to eval?

    Marc Girod, Apr 19, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    164
    Marc Girod
    Apr 19, 2011
Loading...

Share This Page