eval('000052') = 42?

Laurent Pointal laurent.pointal at limsi.fr
Wed Feb 21 03:50:55 EST 2007


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.



More information about the Python-list mailing list