News for hackers. Stuff that matters.
Re:Why Python 3 Exists by harryf on Thursday December 17, 2015 @11:50AM (#10752551)
What makes me snarky is the replacing of

    '%s %s' % ('one', 'two')
With

    '%s %s'.format('one', 'two')
The latter is just more annoying to type. Stupid argument I know but I find myself grumbling to myself every time...
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 %.
Re:Why Python 3 Exists by teach on Thursday December 17, 2015 @12:51PM (#10753001)
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.

Re:Why Python 3 Exists by gluelogic on Thursday December 17, 2015 @12:02PM (#10752624)
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')
Re:Why Python 3 Exists by wyldfire on Thursday December 17, 2015 @12:38PM (#10752899)
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}'
Re:Why Python 3 Exists by teddyh on Thursday December 17, 2015 @03:45PM (#10754403)
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.
Re:Why Python 3 Exists by teddyh on Thursday December 17, 2015 @06:36PM (#10755517)
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.
Re:Why Python 3 Exists by davidism on Thursday December 17, 2015 @11:53AM (#10752565)
It's not replaced, both are perfectly valid. And this addition also exists in Python 2.
Re:Why Python 3 Exists by andreasvc on Thursday December 17, 2015 @04:21PM (#10754646)
No need to get snarky, because the % formatting is still in Python 3. It is also my preferred formatting method.
Re:Why Python 3 Exists by frewsxcv on Thursday December 17, 2015 @12:54PM (#10753026)
Re:Why Python 3 Exists by sevensor on Thursday December 17, 2015 @01:13PM (#10753162)
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.