eval with print stmt

Discussion in 'Python' started by Art, Jul 29, 2004.

  1. Art

    Art Guest

    Hello,

    I can't seem to get eval or compile to accept the print stmt, or any
    complicated statements.

    The documentation I've looked out doesn't explain why this is.

    ex:
    >>> eval("print('foo')")

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<string>", line 1
    print('foo')

    but this works:
    >>> eval('sys.__stdout__.write("foo\\n")')

    foo

    also, I can't get eval to make callable objects. is this possible?
    >>> eval("def foo():\n return 'foo'")

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<string>", line 1
    def foo():
    ^
    SyntaxError: invalid syntax

    I'd like to have a command line parameter be turned into a callable
    statement with an expected environment (a la awk) but I can't seem to
    get this working. (I get the same behavior for compile.

    Thanks,
    A
    Art, Jul 29, 2004
    #1
    1. Advertising

  2. Art

    Mel Wilson Guest

    In article <>,
    (Art) wrote:
    >Hello,
    >
    >I can't seem to get eval or compile to accept the print stmt, or any
    >complicated statements.
    >
    >The documentation I've looked out doesn't explain why this is.


    eval only works with expressions, and `print ...` is a statement.
    So is `def ...` .

    exec should do what you want.

    Regards. Mel.
    Mel Wilson, Jul 29, 2004
    #2
    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 DELAGE
    Replies:
    2
    Views:
    704
  2. Jay
    Replies:
    3
    Views:
    4,596
    William Ryan
    Oct 31, 2003
  3. Replies:
    2
    Views:
    272
  4. David

    Changing drop down select stmt

    David, Oct 20, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    275
    David
    Oct 20, 2006
  5. bigden007

    if..else stmt

    bigden007, Oct 22, 2007, in forum: Python
    Replies:
    5
    Views:
    388
    Bruno Desthuilliers
    Oct 22, 2007
Loading...

Share This Page