I am trying to write a small Python 2.x API to support fetching a
job
by jobNumber
, where jobNumber
is provided as an integer.
Sometimes the users provide ajobNumber
as an integer literal
beginning with 0, e.g. 037537
. (This is because they have been
coddled by R, a language that sanely considers 037537==37537
.)
Python, however, considers integer literals starting with "0" to
be OCTAL, thus 037537!=37537
, instead 037537==16223
. This
strikes me as a blatant affront to the principle of least
surprise, and thankfully it looks like this was fixed in Python
3---see PEP 3127.
But I'm stuck with Python 2.7 at the moment. So my users do this:
>>> fetchJob(037537)
and silently get the wrong job (16223), or this:
>>> fetchJob(038537)
File "<stdin>", line 1
fetchJob(038537)
^
SyntaxError: invalid token
where Python is rejecting the octal-incompatible digit.
There doesn't seem to be anything provided via __future__
to
allow me to get the Py3K behavior---it would have to be built-in
to Python in some manner, since it requires a change to the lexer
at least.
Is anyone aware of how I could protect my users from getting the wrong job in cases like this? At the moment the best I can think of is to change that API so it take a string instead of an int.
Parser/tokenizer.c
should fix this issue. I guess it all depends on how "under the hood you want to get" :) – Disadvantage