473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

eval('000052') = 42?

Hi,
I just tried to do
eval('00052') and it returned 42.
Is this a known bug in the eval function? Or have I missed the way eval
function works?
Thanks
Feb 21 '07 #1
7 1063
Astan Chee wrote:
I just tried to do
eval('00052') and it returned 42.
Is this a known bug in the eval function? Or have I missed the way eval
function works?
String literals beginning with a 0 are in octal.

Besides, using eval for such a narrow case is extremely unwise. Instead
use int:
>>int('00052')
52

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
God grant me to contend with those that understand me.
-- Thomas Fuller
Feb 21 '07 #2
On Feb 21, 3:09 pm, Astan Chee <s...@al.com.auwrote:
Hi,
I just tried to do
eval('00052') and it returned 42.
Is this a known bug in the eval function? Or have I missed the way eval
function works?
Thanks
Eight fives are forty. Forty plus two is forty two. I see no bug here,
only a language design strangeness which can be blamed on the then-
pervasive influence of all things from Bell Labs :-)

BTW, hasn't anyone told you not to play with the eval function? Put it
down. You could catch something nasty.

Feb 21 '07 #3
Astan Chee a écrit :
Hi,
I just tried to do
eval('00052') and it returned 42.
Is this a known bug in the eval function? Or have I missed the way eval
function works?
Thanks
Ad Erik replied, a literal value beginning by 0 is interpreted as an
octal value (and beginning by 0x it is interpreted as hexadecimal value).

You may use int construction from string, which allow to specify a base
(default to ten):
>>int('00052')
52
>>int('00052',10)
52
>>int('00052',8)
42

Note: this avoid possible evaluation of undesired Python expressions...

A+

Laurent.
Feb 21 '07 #4
Astan Chee wrote:
Hi,
I just tried to do
eval('00052') and it returned 42.
Is this a known bug in the eval function? Or have I missed the way eval
function works?
You know, it's just the answer to the ultimate question of Life, the
universe, and everything.

SCNR, nd

Feb 21 '07 #5
On Feb 21, 5:09 am, Astan Chee <s...@al.com.auwrote:
Hi,
I just tried to do
eval('00052') and it returned 42.
Is this a known bug in the eval function? Or have I missed the way eval
function works?
It works just fine. Read up on integer literals.
>>52 #decimal
52
>>052 #octal
42
>>0x52 #hexadecimal
82
Feb 21 '07 #6
On 2007-02-21, André Malo <au********@g-kein-spam.comwrote:
Astan Chee wrote:
>Hi,
I just tried to do
eval('00052') and it returned 42.
Is this a known bug in the eval function? Or have I missed the way eval
function works?

You know, it's just the answer to the ultimate question of
Life, the universe, and everything.
Yeah, but I was hoping the question would've turned out better.

--
Neil Cerutti
Feb 21 '07 #7
On Feb 20, 7:37 pm, "John Machin" <sjmac...@lexicon.netwrote:
On Feb 21, 3:09 pm, Astan Chee <s...@al.com.auwrote:
Hi,
I just tried to do
eval('00052') and it returned 42.
Is this a known bug in the eval function? Or have I missed the way eval
function works?
Thanks

Eight fives are forty. Forty plus two is forty two. I see no bug here,
only a language design strangeness which can be blamed on the then-
pervasive influence of all things from Bell Labs :-)
So is this anachronism slated for removal in Python 3?
Feb 21 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.