[Python-ideas] Disallow "00000" as a synonym for "0"

Ron Adam ron3200 at gmail.com
Sun Jul 19 18:22:49 CEST 2015



On 07/18/2015 11:17 PM, Steven D'Aprano wrote:
> On Sat, Jul 18, 2015 at 03:40:28PM -0400, Ron Adam wrote:
>
>> >And then there is this...
>> >
>>>>> > >>>000.0
>> >0.0
>>>>> > >>>000.1
>> >0.1

> The parsing rules for floats are not the same as int,

Umm.. yes.  But are they intentional different?

> and since floats
> always use decimal, never octal, there's no ambiguity or confusion from
> writing "0000.0000". I don't propose changing floats.

Me either.  It seems to me int's should be relaxed to allow for leading 
zero's instead.

The most common use of leading zero's is when numbers in strings are used 
and those numbers are sorted.  Without the leading zero's the sort order 
may not be in numerical order.

The int class supports using leading zero's in strings, but not in literal 
form.  (Although it may use a C string to int function when parsing it.)

 >>> int("0000")
0
 >>> int("007")
7

 >>> int(0000)
0

 >>> int(007)
   File "<stdin>", line 1
     int(007)
           ^
SyntaxError: invalid token

 >>> int(007.0)
7

 >>> float("0001")
1.0

It's common to cut and paste numerical data from text data.  If you are 
cutting numerical data from a table, then you may also need to remove all 
the leading zeros.  A macro can do that, but it complicates a simple cut 
and paste operation.

Note that this does not effect the internal representation of ints, only 
how python interprets the string literal during the compiling process.


A social reason for this limitation is that a number of other languages do 
use a leading digit 0 to define octal numbers.  And this helps catch silent 
errors due to copying numbers directly in that case.  (Python uses "0o" and 
not just "0".)

In python, it just catches a possible silent error, when a 0onnn is 
mistyped as 00nnn.


So this looks like it's a quick BDFL judgement call to me.  (or other core 
developer number specialist call.)  I think the status quo wins by default 
otherwise.


Cheers,
    Ron



More information about the Python-ideas mailing list