News for hackers. Stuff that matters.
Re:New string formatting in Python by Walkman on Sunday January 03, 2016 @06:32PM (#10833128)
I have mixed feeling about this. This will be the FOURTH way of formatting strings in Python. The other formattings will never go away.
Re:New string formatting in Python by bhaak on Sunday January 03, 2016 @07:14PM (#10833293)
What's the fourth way? The article only mentions three.
Re:New string formatting in Python by rdtsc on Sunday January 03, 2016 @08:45PM (#10833582)
4th way is with templates:

   >>> from string import Template
   >>> s = Template('$who likes $what')
   >>> s.substitute(who='tim', what='kung pao')
This will probably be TIL for many people. It is a surprisingly hidden feature.

I for one, still like the "%s" % x instead of "{0}".format(x). It is simply shorter and I already know or use printf for C in other parts of the project. But with the new f"..." interpolation, I can see liking that more. I am all for being as concise as possible while still being explicit (if that makes any sense at all ;-) ).

Re:New string formatting in Python by toyg on Sunday January 03, 2016 @09:36PM (#10833735)
I've always found templates way overkill for most of my needs, but they cover a specific and legitimate use-case (large and complex blocks of text).

In the same way, .format() solved the problem of inconsistency and bugs happening all the time with %. It was a restriction and formalization effort, trying to root out bad practices and hence slightly more explicit, but it made sense.

The new f'' IMHO does not solve anything beyond pandering to developer laziness. It will likely introduce bugs in places where developers are not clear about the context ("oh, I thought we didn't have a 'x' var at this point, turns out we do!"), because (from what I understand) it takes away the ability to define which variables should be considered.

Luckily it will take a while before it percolates in any significant library, but I sincerely hope it just doesn't gain much traction.

Re:New string formatting in Python by nothrabannosir on Sunday January 03, 2016 @07:44PM (#10833392)
Not the op but Im guessing just using + (which calls .__str__(), iirc?)
Re:New string formatting in Python by richardwhiuk on Sunday January 03, 2016 @08:10PM (#10833480)
+ doesn't call __str__ internally.

e.g the following is an error.

  a = 4
  "a: " + a
You have to do:

  "a: " + str(a)
Re:New string formatting in Python by barely_stubbell on Sunday January 03, 2016 @09:34PM (#10833729)
I agree that + is likely the 4th way.

A minor correction to your comment: "+" calls the __add__ method (big surprise) - just fyi

Re:New string formatting in Python by orionblastar on Sunday January 03, 2016 @07:03PM (#10833245)
I think they keep the other formatting for backward compatibility in legacy code.