Python Gotcha with Octal Numbers

Steve Holden sholden at holdenweb.com
Wed Feb 13 15:49:36 CET 2002


"Christopher Saunter" <Christopher.Saunter at durham.ac.uk> wrote in message
news:a4dt3d$6us$1 at sirius.dur.ac.uk...
> 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?
>
Chris: Welcome to c.l.py! Your point is not unreasonable, but this octal
representation is widely used in the Unix world. Unfortunately, making 0oNNN
the *only* way to represent octal would break far too many programs in which
0100 is *supposed* to mean 64.

It might be possible to introduce it as a redundant alternative, but
generally speaking there's resistance to having more than one way to
represent something (confuses learners), and this wouldn't anyway have saved
you from the glitch you experienced.

Serves you right for being anal about alignment when only the interpreter
and you were going to read the code <0.9 wink>.

regards
 Steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Author, Python Web Programming: http://pydish.holdenweb.com/pwp/

"This is Python.  We don't care much about theory, except where it
intersects with useful practice."  Aahz Maruch on c.l.py







More information about the Python-list mailing list