define a new func on the fly?

Joshua Marshall jmarshal at mathworks.com
Thu Mar 1 22:35:15 EST 2001


Costas Menico <costas at meezon.com> wrote:
> Gregory Jorgensen<gregj at pobox.com> wrote:

>>eval() evaluates an expression. exec() executes a statement. Try this:
>>
>>>>> exec("def f():\n\treturn 'hello'\t")

> When I try the above I get a syntax error... I am using PythonWin. Is
> this a bug?

> Costas


>>>>> f()
>>'hello'
>>
>>>>> eval("f()")
>>'hello'

exec seems to want a newline at the end:

  >>> exec("def f():\n\treturn 'hello'\t")
  Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<string>", line 2
      return 'hello'
                   ^
  SyntaxError: invalid syntax

  >>> exec("def f():\n\treturn 'hello'\n")
  >>> f()
  'hello'



More information about the Python-list mailing list