How is correct use of eval()

Chris Rebert clp2 at rebertia.com
Tue Oct 12 02:38:01 CEST 2010


On Mon, Oct 11, 2010 at 5:26 PM, Nobody <nobody at nowhere.com> wrote:
> On Mon, 11 Oct 2010 11:18:37 -0700, Chris Rebert wrote:
>>> What is correct way to use this function?
>>
>> To not use it in the first place if at all possible (use int(),
>> float(), getattr(), etc. instead,
>
> Use read(). Oh wait, Python doesn't have that. Because parsing literals
> and executing code are like totally the same thing.

import ast
module = compile(raw_input(), "<input>", "exec", ast.PyCF_ONLY_AST)
print ast.dump(module)

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list