Numeric literal syntax (was: Py 2.6 changes)

Patrick Maupin pmaupin at gmail.com
Tue Sep 2 22:31:39 EDT 2008


On Sep 2, 6:35 am, Nick Craig-Wood <n... at craig-wood.com> wrote:
> bearophileH... at lycos.com <bearophileH... at lycos.com> wrote:
> >  It's not just my familiarity, Ada language too uses underscore for
> >  that purpose, I think, so there's a precedent, and Ada is a language
> >  designed to always minimize programming errors, simple code mistakes
> >  too.
>
> And perl also

Add Verilog to that list.  The ability to embed underscores in numeric
literals, which the parser discards, is sometimes very useful in
hardware description, especially when dealing with binary bit vectors
which can sometimes be 32 bits or more long.

Underscores are great.  I have actually wished for this in Python
myself, for those cases when I am doing binary.  Spaces, not so much
-- as others have pointed out, this is error prone, partly because
spaces are "light weight" visually, and partly because the parser does
not currently distinguish between different kinds of whitespace.  I
can't count how often I've forgotten a trailing comma on a line of
items.

To the complaints about the underscores getting in the way -- if the
number is short, you don't need either underscores or spaces, and if
the number is long, it's much easier to count underscores to find your
position than it is to count spaces.  Also, on long numbers (where
this is most useful), the issue with mistaking a number for an
identifier is much less likely to happen in real life.

I think the issue of location sensitivity has already been flogged
enough, but I will give it one last hit -- long numbers, where this is
most useful, are often encountered in domain-specific mini languages,
where the number of digits in each portion of a number might have some
specific meaning.  If the proposal were restricted to "once every 3
digits" or something similar, it would not be worth doing at all.

+1 on the original proposal.

Pat



More information about the Python-list mailing list