Integers with leading zeroes

Steven D'Aprano steve at pearwood.info
Wed Jul 22 04:14:08 CEST 2015


On Wed, 22 Jul 2015 11:10 am, Chris Angelico wrote:

> On Wed, Jul 22, 2015 at 10:55 AM, Steven D'Aprano <steve at pearwood.info>
> wrote:
>>> Sometimes these numbers represent codeblocks of a fixed
>>> number of digits. Always writing those numbers with this
>>> number of digits helps being aware of this. It is also
>>> easier for when you need to know how many leading zero's
>>> such a number has.
>>
>> I'm not sure what you mean here. Python ints don't have a fixed number of
>> digits.
> 
> Sometimes your numbers carry specific payloads or structures. A few
> examples:
> 
> Date: 20150722 [decimal]
> Unix permissions: 10777 [octal]
> MAC address: 0014a466fba9 [hex]

I don't see the relevance of any of those examples. Only the date is
kinda-sort in decimal, the others are in octal and hex and so need to be
written as octal or hex numbers:

perm = 0o10777  # not 25031 as the above will give
addr = 0x0014a466fba9  # the above will give a syntax error


The date example should be a string, not an integer.

today = 20151231
tomorrow = today + 1
assert tomorrow == 20160101  # fails

I guess you can have 0 as Unix permissions, there might even be a 0 MAC
address, but would you write them in decimal as 0000 (etc.) when all the
other perms and addresses are written in oct or hex?

addresses = [
    0x0014a466fba9, 
    0x0014a00b3fb1, 
    000000000000, 
    0x003744a9012a, 
    ]


> In the MAC address example, it doesn't make sense to elide the leading
> zeroes. I can't currently think of a common, real-world example that
> uses decimal and isn't gong to push itself to the full eight digits,
> apart from Northern Territory postcodes with their silly 08nn pattern,
> but I'm sure they exist.

Postcodes, or zip codes, also should be written as strings, even if they
happen to be all digits.

I'm still looking for an example of where somebody would write the int zero
in decimal using more than one 0-digit. While I'm sure they are fascinating
in and of themselves, examples of numbers written as strings, in hex or
octal, non-zero numbers written without leading zeroes, or zero written
with only a single digit don't interest me :-)



-- 
Steven



More information about the Python-list mailing list