[Python-Dev] PEP 515: Underscores in Numeric Literals
Steven D'Aprano
steve at pearwood.info
Wed Feb 10 19:21:27 EST 2016
On Wed, Feb 10, 2016 at 03:45:48PM -0800, Andrew Barnert via Python-Dev wrote:
> On Feb 10, 2016, at 14:20, Georg Brandl <g.brandl at gmx.net> wrote:
>
> First, general questions: should the PEP mention the Decimal constructor? What about int and float (I'd assume int(s) continues to work as always, while int(s, 0) gets the new behavior, but if that isn't obviously true, it may be worth saying explicitly).
>
> > * Trailing underscores are not allowed, because they look confusing and don't
> > contribute much to readability.
>
> Why is "123_456_" so ugly that we have to catch it, when
> "1___2_345______6" is just fine,
It's not just fine, it's ugly as sin, but it shouldn't be a matter for
the parser to decide a style-issue.
Just as we allow people to write ugly tuples:
t = ( 1 , 2, 3 ,4, 5, )
so we should allow people to write ugly ints rather than try to enforce
good taste in the parser. There are uses for allowing multiple
underscores, and odd groupings, so rather than a blanket ban, we trust
that people won't do stupid things.
> or "123e__+456"?
That I would prohibit. I think that the decimal point and exponent sign
provide sufficient visual distinctiveness that putting underscores
around them doesn't gain you anything. In some cases it looks like
you might have missed a group of digits:
1.234_e-89
hints that perhaps there ought to be more digits after the 4.
I'd be okay with a rule "no underscores in the exponent at all", but I
don't particularly see the need for it since that's pretty much covered
by the style guide saying "don't use underscores unnecessarily". For
floats, exponents have a practical limitation of three digits, so
there's not much need for grouping them.
+1 on allowing underscores between digits
+0 on prohibiting underscores in the exponent
> More to the point,
> if we really need an extra rule, and more complicated BNF, to outlaw
> this case, I don't think we want a liberal design at all.
I think "underscores can occur between any two digits" is pretty
liberal, since it allows multiple underscores, and allows grouping in
any size group (including mixed sizes, and stupid sizes like 1).
To me, the opposite of a liberal rule is something like "underscores may
only occur between groups of three digits".
> Also, notice that Swift, Rust, and D all show examples with trailing
> underscores in their references, and they don't look particularly out
> of place with the other examples.
That's a matter of opinion.
--
Steve
More information about the Python-Dev
mailing list