Python 030 == 24 is True, How is that correct? -
this question has answer here:
i came across following problem:
as expect:
>>> [01,02,03] [1, 2, 3]
adding superflous 0
's front of integer n
results in mathematical equivalent of integer 0n
, i.e n
.
however when do:
>>> [030, 031, 031, 031, 030, 031, 031, 032, 031, 032] [24, 25, 25, 25, 24, 25, 25, 26, 25, 26]
this notion proved completly incorrect. tried figure out why case trying check if 030
int:
>>> type(030) <type 'int'>
then thought perhaps, 030
being evaluted 24
, it's instead getting type(24)
. thought looking see what's happening dis
might help:
>>> dis.dis('n=030') 0 jump_forward 12349 (to 12352) 3 delete_slice+1 4 <48> >>> dis.dis('n=30') 0 jump_forward 13117 (to 13120) 3 <48>
this didn't clear why behaviour occurs. so, reson behind behaviour?
and per title:
>>> 030 == 24 true
the leading 0
means octal or base8. defined behaviour python2, due confusion has caused need write 0o30
in python3 write 0x18
hexidecimal number
the 0o30
syntax works in python2.7, it's idea use if wish write octal literals
this why 08
, 09
cause errors - 8 , 9 aren't valid octal digits
Comments
Post a Comment