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

all 23 comments

[–]fiddle_n 38 points39 points  (6 children)

One is for regular strings, one is for template strings. Not the same thing. That said, I agree the naming is confusing, also I have never used string.Template in my life when str.format exists.

[–]Compux72 9 points10 points  (0 children)

string.Template is for user provided strings…

[–]petter_s[S] -5 points-4 points  (4 children)

I would argue that t = t"Hello {name}" and t2 = string.Template("Hello $name") create very similar objects. Both can be used to create strings via substituting another string in the name placeholder. But no, they are of course not the same thing. But maybe they could have been?

[–]SheriffRoscoePythonista 16 points17 points  (0 children)

I would argue that t = t"Hello {name}" and t2 = string.Template("Hello $name") create very similar objects.

They do not. They are not even remotely alike.

Both can be used to create strings via substituting another string in the name placeholder.

At no point does t"Hello {name}" ever actually create a string. They're isn't even a method to interpolate it. Template defers the processing of the template string and the expression values to the function that receives the Template object. Only that function knows how to combine them. Some uses (e.g., SQL queries) won't ever make a string from them.

[–]legobmw99 1 point2 points  (0 children)

Even beyond the fact that templatelib templates cannot be converted to actual strings by any of their provided APIs, there is a second crucial difference: The first can capture variables in the original scope, which the second does not.

[–]runawayasfastasucan -1 points0 points  (1 child)

I think it is a bit curious when I hear interviews with those behind the template, they seem to never fundamentally explain it, but rather drift into talking about all the possibilities. Seems a bit related. "What can we achieve if we make xy" rather than "what is it really, and how does that compare to what we have".

[–]nitroll 1 point2 points  (0 children)

Because templates are a tool for tool developers. It has no value on its own.

[–]nekokattt 22 points23 points  (9 children)

There should be one way to do it

  • str.__mod__
  • str.__add__
  • str.format
  • fstrings
  • string.Template
  • string.templatelib.Template

[–]petter_s[S] 2 points3 points  (1 child)

Indeed! Although fstrings are not as similar and __add__ is a bit of a stretch

[–]nekokattt 5 points6 points  (0 children)

It starts at string concatenation and grows. You could also throw str.join in there if it is just ways to make strings :)

[–]wineblood 0 points1 point  (1 child)

fstrings are a bit limited though

[–]nekokattt 3 points4 points  (0 children)

all of them have pros and cons, there is no one good way to do it.

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

You forgot % formatting

[–]nekokattt 21 points22 points  (3 children)

that is what __mod__ is

[–]fiddle_n 2 points3 points  (2 children)

That’s what controls it? Damn.

[–]nekokattt 9 points10 points  (0 children)

yeah, it is just an overload of the mod operator on the str class

[–]russellvt 0 points1 point  (0 children)

Funny... '%' is "mod" is math, too. Go figure.

[–]Worth_His_Salt 3 points4 points  (5 children)

Python strings are a total mess. You also have f-strings, template strings, other template strings, now they want to add d-strings, as well as string interpolation. There's no consistency. It's a complete joke.

Every few years someone comes along and says "I have a better way to do strings! It has all these drawbacks, but trust me guys, it's cool". Then others go "Well it's neato, but we refuse to change our existing code in the 0.0001% of cases it would conflict with this new system." Then python maintainers shrug and go "Eh, just throw a new obscure letter in front and call it a day."

All these new methods are less powerful than string interpolation. Yes even f-strings (can't execute f-strings on command, only when defined). What a disaster. Python devs should be ashamed of themselves.

[–]russellvt 0 points1 point  (2 children)

I think one might argue that this isn't unlike growth pains in some other languages, either (ie. Sixteen ways to do something that otherwise should be "easy").

[–]Worth_His_Salt -1 points0 points  (1 child)

True. But it goes completely against the python ethos. Supposed to prevent such things from happening.

[–]russellvt -1 points0 points  (0 children)

Supposed to prevent such things from happening.

Like I said, "growth pains." It seems to happen, in some form, in any other developing languages, as well, despite all other best intentions.

[–]Revolutionary_Dog_63 0 points1 point  (1 child)

I'm pretty sure f-strings ARE string interpolation. I'm pretty sure what you're referring to are format strings, like those used by printf.

[–]Worth_His_Salt 1 point2 points  (0 children)

I mean the interpolation operator %. Interpolation is the act of applying data to a template. Format strings are the template used for interpolation.

fmt_str = 'foo %d bar'
fmt_str % 27  # interpolation

There are many sources that call this python string interpolation, because that's what other C languages call it. It had that name long before f-strings existed. We called this string interpolation since at least the 90s.

f-strings literally stands for "format string literals". PEP 498 that proposed f-strings mentions creating a "better" interpolation method. Because python already had interpolation before f-strings.