help me

Peter L Hansen peter at engcorp.com
Sat Oct 9 22:56:28 EDT 2004


dataangel wrote:
> Marius Bernklev wrote:
>> chrispatton at gmail.com (Chris Patton) writes:
>>> Hey everybody. I am writing a program that requires the user to make
>>> the name for variables inside the program. For example:
>>>>>> exec raw_input()+' = 34'
>>>
>>> However, I don't want to have to use the "exec" statement. Thanks for
>>> any help!
>> Are you sure you don't want to use a plain dict for this?  And if so, 
>> why?
>>
> Why don't you want to use exec? Just curious. That's the normal way to 
> get a string to python code as far as I know.

No, it's not the normal way, it's the crude and insecure way.

The normal way is to use the string as a key to a dictionary,
perhaps locals() or globals(), as Marius appears to be
suggesting.

Like regular expressions, in the hands of a beginner exec
and eval() lead to code that is one or more of dangerous,
unreadable, unmaintainable, awkward, or just plain sloppy.

-Peter



More information about the Python-list mailing list