Python Gotcha with Octal Numbers

Christopher Saunter Christopher.Saunter at durham.ac.uk
Wed Feb 13 09:26:53 EST 2002


Greetings All!

I have been using Python now for arround a year, and have found it
incredably easy to learn the language, primarily because things are very
obvious cf other languages, and until the other day, I had never had a
'Gotcha' - a bug caused by me entering some code wrongly and not realising
it.

It all stems to this, I was assigning to numbers to variables:

var1 = 1000
var2 = 0100
etc.

When I ran the code, things did not go as they sould.  Some investigation
(running the code line by line in the interpreter...) revealed the problem
to be with asigning a value of 0100 to a variable.  (I just put the 0 in
there to ballance the screen.  Daft I know...)

>From the interpreter:
>>> 0100
64

I missed the bit in the docs where prefacing a number with 0 means you are
entering in octal...

Personally, I would like to see octal number entry migrated from 0--- to
something like 0o---.  This would bring things more in line with the entry
of hex numbers (0x---)

I know it's only a minor thing, but it'd be nice if my numbers behaved as
I expected...

Opinions?

Regards,
	Chris Saunter



More information about the Python-list mailing list