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

Andrew Barnert abarnert at yahoo.com
Thu Feb 11 20:38:26 EST 2016


On Thursday, February 11, 2016 10:35 AM, Jeff Hardy <jdhardy at gmail.com> wrote:

>On Thu, Feb 11, 2016 at 10:15 AM, Andrew Barnert via Python-Dev <python-dev at python.org> wrote:
>
>>That's a good point: we need style rules for PEP 8.


...
>>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)
>

>I imagine that for whatever "bad" grouping you can suggest, someone, somewhere, has a legitimate reason to use it. 

That's exactly why we should just have bad examples in the style guide, rather than coming up with style rules that try to strongly discourage them (or making them syntax errors).

>Any rule more complex than "Use underscores in numeric literals only when the improve clarity" is unnecessarily prescriptive.

Your rule doesn't need to be stated at all. It's already a given that you shouldn't add semantically-meaningless characters anywhere unless they improve clarity....

I don't think saying that they're for "visually separating meaningful digit groups like thousands, bytes, and the like" is unnecessarily prescriptive. If someone comes up with a legitimate use for something we've never anticipated, it will almost certainly just be a way of grouping digits that's meaningful in a way we didn't anticipate. And, if not, it's just a style guideline, so it doesn't have to apply 100% of the time. If someone really comes up with something that has nothing to do with grouping digits, all the style guideline will do is make them stop and think about whether it really is a good use of underscores--and, if it is, they'll go ahead and do it.


More information about the Python-Dev mailing list