Re:Why Python 3 Exists
by harryf on Thursday December 17, 2015 @11:50AM
(#10752551)
What makes me snarky is the replacing of
With
The latter is just more annoying to type. Stupid argument I know but I find myself grumbling to myself every time...
[ Parent
| on HN ]
Re:Why Python 3 Exists
by RussianCow on Thursday December 17, 2015 @11:55AM
(#10752579)
The second example should be:
'{} {}'.format('one', 'two')
Either way, the former example still works in Python 3.5, so that syntax hasn't gone away. `format` is preferred, though. This Stack Overflow question has some good answers as to why: http://stackoverflow.com/questions/5082452/python-string-for...
Re:Why Python 3 Exists
by dietrichepp on Thursday December 17, 2015 @12:01PM
(#10752622)
You mean,
'{} {}'.format('one', 'two')
Your experience may vary, but in my experience, when I switched to .format(), I found a number of bugs in code that used % instead. As mentioned, you can continue using %.I had the same experience.
I love love love printf() and it's ilk, so switching to something else (no matter how well designed) seemed asinine at first.
But .format() has really started to grow on me and did uncover some subtle bugs in old code.
The thing about the latter form is that you can do something like '{0} {1} {0} {2}'.format('apple', 'banana', 'orange') which results in 'apple banana apple orange'
Re:Why Python 3 Exists
by squeaky-clean on Thursday December 17, 2015 @12:24PM
(#10752790)
You can also use keyword arguments, which is really helpful in certain situation. Example from the docs:
'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')You could do that before with % substitution too. But I prefer .format() because (1) it's the new idiom and (2) will coerce to string without me binding the type in the format string.
Re:Why Python 3 Exists
by mixmastamyk on Thursday December 17, 2015 @12:27PM
(#10752816)
Simplified to this in Py 3.6:
f'{one} {two}'Do you have a reference for that? Wouldn’t that be introducing a huge security hole in all programs?
Re:Why Python 3 Exists
by mixmastamyk on Thursday December 17, 2015 @03:51PM
(#10754451)
PEP 498. Literals only, does not add any additional security problems.
Ah, right; I missed the ‘f’ prefix. And since it’s only done when parsing the expression, it is not a security problem. Thanks!
Re:Why Python 3 Exists
by kazagistar on Thursday December 17, 2015 @03:50PM
(#10754438)
Is it actually happening? Proper string interpolation? In all its extendable glory?
Re:Why Python 3 Exists
by mixmastamyk on Thursday December 17, 2015 @03:53PM
(#10754462)
Pep was accepted, and I believe can be tried in a nightly or alpha release. It is not extensible though, GvR didn't see much utility, yet at least.
It's not replaced, both are perfectly valid. And this addition also exists in Python 2.
No need to get snarky, because the % formatting is still in Python 3. It is also my preferred formatting method.
Let's not also forget string.Template ;)
https://docs.python.org/2/library/string.html#string.Templat...
I actually found a good use for string.Template, but that doesn't mean I understand why it exists in addition to the other two formatting sub-languages.
Re:Why Python 3 Exists
by mixmastamyk on Thursday December 17, 2015 @02:41PM
(#10753905)
It was created long before .format and is better in some cases for i18n where simplicity is best.
Re:Why Python 3 Exists
by RodericDay on Thursday December 17, 2015 @07:59PM
(#10755895)
The super, super controversial automatic interpolation feature is what I'm really looking forward to.