[Tutor] name is not defined error (fwd)

Kent Johnson kent37 at tds.net
Mon Oct 30 12:47:12 CET 2006


Danny Yoo wrote:
> 
> ---------- Forwarded message ----------
> Date: Sun, 29 Oct 2006 12:33:58 -0500
> From: Kristinn Didriksson <kdidriksson at gmail.com>
> To: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
> Subject: Re: [Tutor] name is not defined error
> 
> Hi Danny,
> Thanks :)
> The program works now. I'll take a look at those threads.
> Here is what the book says " This function (eval) takes any string and 
> evaluates it as if it were a Python expression." I took that to mean that it 
> would turn a sting into an integer. Oops.

eval() evaluates a Python expression and returns a value. This is 
similar to what happens when you enter text on the command line. eval() 
will take a string like '123' and turn it into the integer 123. eval() 
will also take the string 'import shutil;shutil.rmtree("/")' and turn it 
into a really bad day. A better way to convert a string that represents 
an integer into an integer is to use the int() function.

Danny's warnings apply, I just wanted to clarify eval() a bit.

Kent

> it is a little confusing to get started, but lots of practice will change that.
> Thanks for your help.
> Regards,
> Kristinn
> 
> On Oct 29, 2006, at 12:15 PM, Danny Yoo wrote:
> 
>>
>>> It is my understanding that eval turns a string into a number and then I can 
>>> do math operations on it.
>> Hi Kristinn,
>>
>> No, no, that's not what it does.  *grin*
>>
>> Where did you read about eval() from?  If you saw it in a beginner's 
>> tutorial, tell us who the guilty party is so we can knock some sense into 
>> that tutorial.
>>
>> Don't use eval(): it's "unsafe" in the sense that it does something much more 
>> than what one might expect. I won't say too much more about it at the moment, 
>> but if you're interested, see the threads:
>>
>>    http://mail.python.org/pipermail/tutor/2001-September/008967.html
>>    http://mail.python.org/pipermail/tutor/2004-December/033828.html
>>
>> for examples.  Again, I have to say this again: don't use eval() unless you 
>> really know what you're doing.
>>
>>
>> Anyway, I think you're looking for the ord() function:
>>
>>    http://docs.python.org/lib/built-in-funcs.html#l2h-55
>>
>> Its inverse is the chr() function:
>>
>>    http://docs.python.org/lib/built-in-funcs.html#l2h-15
>>
>> Good luck!
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 




More information about the Tutor mailing list