FAQ: How do I calculate what quoted strings and numbers mean?
p.lavarre at ieee.org
p.lavarre at ieee.org
Wed Nov 15 03:25:22 EST 2006
> A FAQ that discusses good ways to handle Python-like literals and
> expressions would definitely be a useful addition to the FAQ. if nobody
> else does anything about it, I'll get there sooner or later.
Thank you.
> > eval(source, {'builtins': {}}) works enough like an evaluator of
> > literals to ...
>
> eval(source, {'builtins': {}}) doesn't prevent you from using built-ins,
> though. it's spelled __builtins__, not builtins:
>
> >>> eval("len('10')", {"builtins": {}})
> 2
> >>> eval("len('10')", {"__builtins__": {}}
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<string>", line 1, in <module>
> NameError: name 'len' is not defined
Grin. Indeed, newbie me, I didn't know that either, thank you.
I was happy enough when I saw an improvement like:
>>> import os
>>> result = eval("os.system('pwd')")
.../Desktop
>>> result = eval("os.system('pwd')", {"whatever": {}})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'os' is not defined
>>>
Now I fear that I must have copied the misspelled builtins from some
page that's still out there somewhere misleading people ...
More information about the Python-list
mailing list