Error evaluating numbers starting in zero

Alex Martelli aleaxit at yahoo.com
Mon Dec 18 06:43:35 EST 2000


"Mike Brenner" <mikeb at mitre.org> wrote in message
news:mailman.977137021.11074.python-list at python.org...
> >> I have a python checkbook program that evaluates date strings, such
> >> as "01/31/99". Most dates are fine, but if I have a date such as
> >> "01/08/99", python runs into an error when it tries to eval("08").
>
> > Use int() instead of eval().
>
>
> So, that may also explain the failure with
>
> i = 08
>
> and with reading in numbers,
> the python interpretor must be using EVAL instead of INT?

If it did otherwise, then
    i = 010
would wrongly give i a numerical value of ten, rather than
the value of eight it's *supposed* to.

It may or may not be wise to have the 0<digits> notation
for octal that the C language (AFAIK) pioneered; I, quite
personally, think it's not wise, since leading-zeros should
not affect a numeric literal meaning to such an extent, and
it's a hard feature to explain to complete beginners -- a
different syntax might have been a wiser choice right from
the start.  But compatibility constraints force Python to
keep it, whatever else one thinks of it, until perhaps the
mythical future 'Python 3000' that WILL be allowed to break
backwards compatibility.

Given that 0<digits> indicates an octal number, it's surely
best to diagnose digit-strings that are an error in that
context, rather than gobble them down with some halfway
sensible 'interpretation' (as some very old C compilers
used to do...).


Alex






More information about the Python-list mailing list