code blocks

Ian Kelly ian.g.kelly at gmail.com
Sun May 10 23:36:38 EDT 2015


On Sun, May 10, 2015 at 9:31 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Sun, May 10, 2015 at 7:39 PM, zipher <dreamingforward at gmail.com> wrote:
>> Similarly, you'd want:
>>
>>>>> encode(codestr)
>>
>> to instantiate all objects in the codestr.  You can't do this with eval, because it doesn't allow assignment (eval(n=2) returns "InvalidSyntax").
>
> Is exec what you're looking for?
>
>>>> exec('n = 2')
>>>> print(n)
> 2

I just found that eval can be used to evaluate a code object compiled
in the 'exec' mode:

>>> eval(compile('n = 42', '', 'exec'))
>>> n
42

Interesting. But I suppose that the mode is really just a compilation
option and there's not really much distinction as far as eval is
concerned once they've been compiled.



More information about the Python-list mailing list