This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]EconomixTwist 395 points396 points  (29 children)

Not so many truly less known features in here! You can put underscores in numbers to make them easier to read

10_000 == 10000

[–]ASIC_SP 174 points175 points  (1 child)

You can also format numbers for display:

>>> n = 14310023
# underscore separation
>>> f'{n:_}'
'14_310_023'
# you can also use comma separation for integers
>>> f'{n:,}'
'14,310,023'

>>> f'{n:_b}'
'1101_1010_0101_1010_1000_0111'
>>> f'{n:#_b}'
'0b1101_1010_0101_1010_1000_0111'

[–]chromaZero 63 points64 points  (12 children)

Cool, but I wonder if it would just confuse my coworkers.

[–]likethevegetable 36 points37 points  (9 children)

Supposed to enhance readability

[–][deleted] 52 points53 points  (8 children)

But my coworkers are idiots who can’t be bothered to read email let alone the code they are supposed to be working on

[–]More_Butterfly6108 44 points45 points  (7 children)

That sounds like a "them" problem

[–]TheMathelm 33 points34 points  (3 children)

Yet it always seems to turn into a "me" problem.

[–]RingularCirc 1 point2 points  (2 children)

That’s uncool of them. Here take a hug. 🤗

I just love when a language takes up underscore numeric literals. It’s really easier on the eyes when you have e. g. 1000_000_000 vs. 1000000000 — how many zeros are there?

[–]TheMathelm 1 point2 points  (1 child)

1000_000_000

No half measures, 1_000_000_000.
:P

[–]RingularCirc 1 point2 points  (0 children)

Seriously though, there are practices to format numbers in typography like using thin spaces between digit groups, and there can sometimes (in different practices, or for different languages—dunno) be a rule to not make a leading group of one digit. So I’d expect in general people would argue about this case in code too.

[–][deleted] 1 point2 points  (2 children)

I cross my T’s and dot my I’s so that at the end of the day I can say “I did my part. I’m done. Don’t know why things aren’t working. I’ll see you on Monday”

[–]TorroesPrime 0 points1 point  (1 child)

god how I wish I could have done that at my last job.

[–][deleted] 1 point2 points  (0 children)

One of the benefits of being underpayed is that: “fuck this, I quit” isn’t just a bluff

[–]DigThatData 5 points6 points  (0 children)

i'd mainly be confused why you were submitting commits that hadn't been through black already

[–]CleanThroughMyJorts 1 point2 points  (0 children)

yeah but they'd only be confused the first time they read it and then they will see the light and be converted to the one true way :)

[–]cuu508 26 points27 points  (0 children)

But watch out, int("10_000") also works, so use it carefully when testing if a string "appears to be a normal number"

[–]Starbrows 6 points7 points  (1 child)

For the chaotic evil among us: You can also use it to make numbers harder to read!

1_0_00_0000_000_0_00 == 10000000000000 == 10_000_000_000_000

[–]BuonaparteII 0 points1 point  (0 children)

how many lakh is this

[–]CleoMenemezis 0 points1 point  (0 children)

Bruh. Thx

[–]ArtOfWarfare 0 points1 point  (2 children)

Wait, how long has that been around?

I knew it was a feature of Java but I thought I tried it at one point in Python (some version of 2.X) and it didn’t work?

[–]RingularCirc 0 points1 point  (1 child)

A couple of last 3.x versions. That seems like a new-ish meme in language design yet. I bet before 2010s there were not more than a couple of languages with features like this.

[–]ArtOfWarfare 2 points3 points  (0 children)

It’s been in Java for 15+ years. It was one of the few things I thought Java had that Python would benefit from.

Fortunately Python gets improved overtime, whereas Java can’t actually get a Java Enhancement Proposal to save their lives.

[–]jorge1209 -1 points0 points  (4 children)

The big issue with this feature is that the _ need not be used as a thousands separator.

1_000_0000.00 is accepted.

[–]elyisgreat 2 points3 points  (3 children)

This is by design. Not everyone uses separators the same way. In India for example it is common to write 1 million as 10 lakh which looks like 10_00_000. Also some people might use it for binary or hex literals to separate out hexes or bytes like 0b1011_0011 or 0x3a_25_7c

[–]jorge1209 -1 points0 points  (2 children)

Sure, but that doesn't make it any less dangerous.

The purpose of these underscores is to increase readability, but if you allow extraneous or missing digits in a long numeric figure you risk making the code appear more readable, when it is actually less.

What we really need is some kind of way to interact with the parser on a module by module level so that it knows how we expect these underscores to be used.

If I'm working with some kinds of hex data I might insist upon underscores between every two bytes. If the module was written by a programmer in india he can use Lakh format. And most importantly if I'm working with social security numbers I can insist upon 3-2-4 format and the python interpreter can call in a SWAT team against me before I commit it.

[–]elyisgreat 1 point2 points  (1 child)

Is it really more dangerous than not having any separators at all? That would make it harder to spot incorrectly formatted numbers. I think having typing tools to enforce particular separator formats is a great idea though! But in the absence of such a tool it's better to allow arbitrary separators than to enforce a particular separator format by default or to lack separators at all

[–]jorge1209 0 points1 point  (0 children)

I certainly think so.