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

vasudevram vasudevram at gmail.com
Sun Jun 24 15:22:13 EDT 2007


On Jun 24, 6:28 am, Jean-Paul Calderone <exar... at divmod.com> wrote:
> On Sun, 24 Jun 2007 11:17:40 +1000, Steven D'Aprano <s... 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

Thanks, all. Will check out the replies given.
- Vasudev




More information about the Python-list mailing list