[Python-Dev] PEP 515: Underscores in Numeric Literals

Jeff Hardy jdhardy at gmail.com
Thu Feb 11 13:35:42 EST 2016


On Thu, Feb 11, 2016 at 10:15 AM, Andrew Barnert via Python-Dev <
python-dev at python.org> wrote:

> On Feb 11, 2016, at 09:39, Terry Reedy <tjreedy at udel.edu> wrote:
> >
> > If trailing _ is allowed, to simplify the implementation, I would like
> PEP 8, while on the subject, to say something like "While trailing _s on
> numbers are allowed, to simplify the implementation, they serve no purpose
> and are strongly discouraged".
>
> That's a good point: we need style rules for PEP 8.
>
> But I think everything that's just obviously pointless (like putting an
> underscore between every pair of digits, or sprinkling underscores all over
> a huge number to make ASCII art), or already handled by other guidelines
> (e.g., using a ton of underscores to "line up a table" is the same as using
> a ton of spaces, which is already discouraged) doesn't really need to be
> covered. And I think trailing underscores probably fall into that category.
>
> It might be simpler to write a "whitelist" than a "blacklist" of all the
> ugly things people might come up with, and then just give a bunch of
> examples instead of a bunch of rules. Something like this:
>
> While underscores can legally appear anywhere in the digit string, you
> should never use them for purposes other than visually separating
> meaningful digit groups like thousands, bytes, and the like.
>
>     123456_789012: ok (millions are groups, but thousands are more common,
> and 6-digit groups are readable, but on the edge)
>     123_456_789_012: better
>     123_456_789_012_: bad (trailing)
>     1_2_3_4_5_6: bad (too many)
>     1234_5678: ok if code is intended to deal with east-Asian numerals
> (where 10000 is a standard grouping), bad otherwise
>     3__141_592_654: ok if this represents a fixed-point fraction
> (obviously bad otherwise)
>     123.456_789e123: good
>     123.456_789e1_23: bad (never useful in exponent)
>     0x1234_5678: good
>     0o123_456: good
>     0x123_456_789: bad (3 hex digits is usually not a meaningful group)
>
> The one case that seems contentious is "123_456_j". Honestly, I don't care
> which way that goes, and I'd be fine if the PEP left out any mention of it,
> but if people feel strongly one way or the other, the PEP could just give
> it as a good or a bad example and that would be enough to clarify the
> intention.
>

I imagine that for whatever "bad" grouping you can suggest, someone,
somewhere, has a legitimate reason to use it. Any rule more complex than
"Use underscores in numeric literals only when the improve clarity" is
unnecessarily prescriptive.

- Jeff
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20160211/ea0e2ece/attachment-0001.html>


More information about the Python-Dev mailing list