eval or execute, is this the (most) correct way ?

Stef Mientki stef.mientki at gmail.com
Mon Aug 11 17:53:15 EDT 2008


Martin v. Löwis wrote:
>> So AFAIK, sometimes I've to use eval and sometimes I need exec,
>> so I use the following code (global / local dictionary parameters are
>> left out in this example):
>>
>>
>> Is this the (most) correct / elegant way, or are there better solutions ?
>>     
>
> You should be using compile with the "single" start symbol, and then
> use eval on the resulting code option.
>
>   
thanks Martin,
but when I read the doc (of one of the many) "compile" functions,
I see 2 problems:
- I still have to provide "kind" as exec or eval
- I can not specify the global and local namespace (which is essential 
for me)
>> I read somewhere that exec is going to disappear,
>>     
>
> That's not true. exec stops being a statement, and becomes a function
> (like print).
>
>   
That's good to hear,
as I already didn't realize it could also be used as a statement ;-)

cheers,
Stef

> Regards,
> Martin
> --
> http://mail.python.org/mailman/listinfo/python-list
>   




More information about the Python-list mailing list