Annoying octal notation

Derek Martin code at pizzashack.org
Sun Aug 23 05:19:01 CEST 2009


On Sat, Aug 22, 2009 at 02:55:51AM +0000, Steven D'Aprano wrote:
> > I can see how 012 can
> > be confusing to new programmers, but at least it's legible, and the
> > great thing about humans is that they can be taught (usually).
> 
> And the great thing is that now you get to teach yourself to stop writing 
> octal numbers implicitly and be write them explicitly with a leading 0o 
> instead :)

Sorry, I don't write them implicitly.  A leading zero explicitly
states that the numeric constant that follows is octal.  It is so in 6
out of 7 computer languages I have more than a passing familiarity
with (the 7th being scheme, which is a thing unto itself), including
Python.  It's that way on Bourne-compatible and POSIX-compatible Unix
shells (though it requires a leading backslash before the leading zero
there).  I'm quite certain it can not be the case on only those 6
languages that I happen to be familiar with...

While it may be true that people commonly write decimal numbers with
leading zeros (I dispute even this, having to my recollection only
recently seen it as part of some serial number, which in practice is
really more of a string identifier than a number, often containing
characters other than numbers), it's also true that in the context of
computer programming languages, for the last 40+ years, a number
represented with a leading zero is most often an octal number.  This
has been true even in Python for nearly *twenty years*.  Why the
sudden need to change it?

So no, I don't get to teach myself to stop writing octal numbers with
a leading zero.  Instead, I have to remember an exception to the rule.

Also I don't think it's exactly uncommon for computer languages to do
things differently than they are done in non-CS circles.  A couple of
easy examples: we do not write x+=y except in computer languages.  The
POSIX system call to create a file is called creat().  If you think
about it, I'm sure you can come up with lots of examples where even
Python takes liberties.  Is this a bad thing?  Not inherently, no.
Will it be confusing to people who aren't familiar with the usage?
Quite possibly, but that is not inherently bad either.  It's all about
context.

> Use of octal isn't common. 

It's common enough.  Peruse the include files for your C libraries, or
the source for your operating system's kernel, or system libraries,
and I bet you'll find plenty of octal.  I did.  [Note that it is
irrelevant that these are C/C++ files; here we are only concerned with
whether they use octal, not how it is represented therein.]  I'd guess
there's a fair chance that any math or scientific software package
uses octal.  Octal is a convenient way to represent bit fields that
naturally occur in groups of 3, of which there are potentially
limitless cases.  

> You've given two cases were octal notation is useful, but for every
> coder who frequently writes umasks on Unix systems, there are a
> thousand who don't.

I gave two cases that I use *daily*, or very nearly daily.  My hats
currently include system admin, SQA, and software development, and I
find it convenient to use octal in each of those.  But those are
hardly the only places where octal is useful.  Have a look at the
ncurses library, for example.  Given that Python has an ncurses
interface, I'm guessing it's used there too.  In fact if the Python
source had no octal in it, I would find that very surprising.

> It's no hardship to write 0o12 instead of 012.

Computer languages are not write-only, excepting maybe Perl. ;-)
Writing 0o12 presents no hardship; but I assert, with at least some
support from others here, that *reading* it does.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20090822/c51dabba/attachment.pgp>


More information about the Python-list mailing list