4 ms·
> PEP 515 adds the ability to use underscores in numeric literals for improved readability. For example: >>> 1_000_000_000_000_000 1000000000000000
by happy-go-lucky 10y ago
> PEP 515 adds the ability to use underscores in numeric literals for improved readability. For example:
>>> 1_000_000_000_000_000
1000000000000000
>>> 0x_FF_FF_FF_FF
4294967295
> Single underscores are allowed between digits and after any base specifier. Leading, trailing, or multiple underscores in a row are not allowed.
- Qantourisc 10y agoEvery language should have this.
- dbcurtis 10y agoI totally agree with you. But that opinion reflects two kinds of programming that make up 75% of what I do -- either close to the metal, or comms protocol implementations. I was at PyCON 2016 when Guido was reading off the list of 3.6 new features. In a room full of many 100's of people, I was the only one (that I could hear) applauding for underscores. Guido himself was very dismissive of the feature and it was clear he thought it pointless, but OTOH harmless so why not let it in. I suspect there is a strong correlation between programmers that want underscores in numeric literals, and programmers that live and breath hex.
- simonh 10y agoNice. I really like the look of f-strings as well. All the power of str.format() with none of the verbosity. >>> name = "Fred" >>> f"He said his name is {name}." 'He said his name is Fred.'
- happy-go-lucky 10y agoIt's getting better with each release :)
- hyperbovine 10y agoSurprisingly these are very controversial. A bunch of people seem to hate them. I don't understand why.
- masklinn 10y agoI dislike them because not only are they one more string-formatting method (they're similar to but not actually identical to format-strings as you can use arbitrary expressions in f-string placeholders) and more importantly they're the first one which does not work at all for i18n or more generally for deferred interpolation (which is also used by `logging`)
- orf 10y ago> importantly they're the first one which does not work at all for i18n But surely i18n/deferred interpolation is a corner case, not the norm. If you need that then use what you are currently using, nothings changed.
- JelteF 10y agoNot sure, why you think they cannot be used with defered interpolation. You, can easily combine the two like this: >>> f'{"fstring"} and {{deferred}}'.format(deferred=1234) 'fstring and 1234'
- mixmastamyk 10y agoThere was a large attempt to get i18n/deferred but Guido decided to keep it simple for this release. Perhaps it can be extended in the future. In the meantime i18n uses string.Template.
- yen223 10y agoThere are already three ways to do string interpolation. Adding a fourth way goes against the whole "only one way to do things" aspect of the Zen of Python.
- JelteF 10y agoThe "only one way to do things" is quite low in the Zen of python. It is preceded by for instance "Beautiful is beter than ugly", "Readability counts" and "practicality beats purity" [1]. There's always a trade of in these rules. In this case I think the right choice was made to add this easier way of doing string interpolation for cases where it can be used, also "Now is better than never". [1] Python 3.5.2 (default, Nov 7 2016, 11:31:36) [GCC 6.2.1 20160830] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! >>>
- Analemma_ 10y agoC# got these recently too (it uses $ instead of f, but is otherwise identical), and I love it. The first thing I did when our build tools supported C# 6 was convert almost all string.Format() statements in our repo to use it. Why wouldn't you?
- deleted 10y ago[deleted]
- fithisux 10y agoI don't do Python, but PEP 515 has got it all wrong.