Do eval() and exec not accept a function definition? (like 'def foo: pass) ?

Jean-Paul Calderone exarkun at divmod.com
Sat Jun 23 21:28:06 EDT 2007


On Sun, 24 Jun 2007 11:17:40 +1000, Steven D'Aprano <steve at remove.this.cybersource.com.au> wrote:
>On Sat, 23 Jun 2007 19:58:32 +0000, vasudevram wrote:
>
>>
>> Hi group,
>>
>> Question: Do eval() and exec not accept a function definition? (like
>> 'def foo: pass) ?
>
>eval() is a function, and it only evaluates EXPRESSIONS, not code blocks.

Actually, that's not exactly true:

    >>> x = compile('def foo():\n\tprint "hi"\n', '<stdin>', 'exec')
    >>> l = {}
    >>> eval(x, l)
    >>> l['foo']()
    hi
    >>>

Jean-Paul



More information about the Python-list mailing list