[Python-Dev] PEP 515: Underscores in Numeric Literals (original) (raw)

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. 123456789012: ok (millions are groups, but thousands are more common, and 6-digit groups are readable, but on the edge) 123456789012: better 123456789012: bad (trailing) 123456: bad (too many) 12345678: ok if code is intended to deal with east-Asian numerals (where 10000 is a standard grouping), bad otherwise 3_141592654: ok if this represents a fixed-point fraction (obviously bad otherwise) 123.456789e123: good 123.456789e123: bad (never useful in exponent) 0x12345678: good 0o123456: good 0x123456789: bad (3 hex digits is usually not a meaningful group) The one case that seems contentious is "123456j". 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.



More information about the Python-Dev mailing list