Annoying octal notation

James Harris james.harris.1 at googlemail.com
Thu Aug 20 19:59:14 EDT 2009


On 20 Aug, 20:06, David <71da... at libero.it> wrote:

> Hi all,
>
> Is there some magic to make the 2.x CPython interpreter to ignore the
> annoying octal notation?
> I'd really like 012 to be "12" and not "10".

This is (IMHO) a sad hangover from C (which took it from B but not
from BCPL which used #<octal> and #x<hex>) and it appears in many
places. It sounds like you want to use leading zeroes in literals -
perhaps for spacing. I don't think there's an easy way. You just have
to be aware of it.

Note that it seems to apply to integers and not floating point
literals

>>> 012
10
>>> int("012")
12
>>> 012.5
12.5
>>>

This daft notation is recognised in some surprising places to catch
the unwary. For example, the place I first came across it was in a
windows command prompt:

s:\>ping 192.168.1.012
Pinging 192.168.1.10 with 32 bytes of data:

On B's use of the leading zero see

  http://cm.bell-labs.com/cm/cs/who/dmr/kbman.html

and note the comment: "An octal constant is the same as a decimal
constant except that it begins with a zero. It is then interpreted in
base 8. Note that 09 (base 8) is legal and equal to 011."

It maybe made sense once but this relic of the past should have been
consigned to the waste bin of history long ago.

James



More information about the Python-list mailing list