4 ms·
> One of those ideas that's so obvious in hindsight, you can't believe it took us this long to come up with it. If by "come up with" you mean finally acquiesce
by kbenson 10y ago
> One of those ideas that's so obvious in hindsight, you can't believe it took us this long to come up with it.
If by "come up with" you mean finally acquiesced after decades and decided that string interpolation wasn't such a bad idea... sure.
- StavrosK 10y agoWhat's the difference between `"%s" % foo` and `"{}".format(foo)`? Why is one interpolation and the other not?
- nemetroid 10y agoPython 3.6 is adding `f"{foo}"`, which is rather different than the previous offerings.
- kbenson 10y agoYes, but to clarify, it's only rather different than previous python offerings. There exist things more or less equivalent in other languages, so it's not as if there's much of a reason it took this long.
- nilved 10y agoWow that is horrible.
- deleted 10y ago[deleted]
- allendoerfer 10y agoI really hate the fact that there are now 3 ways to do it. And that it looks like PHP. To be honest, to me the "%" seems the most pythonic way to do it. Format does not seem easier to understand to me . String formatting just cannot be transformed to natural language. I at least don't think "I need to format this string", I think "this variable needs to be in there", in the in there might as well be called "%". I think ["a is %s" % a for a in all_as] just fits. Although named parameters are nicer with format. In no way should there be a third way to do it. Isn't that the basic principle of Python, to care about things like this? I mean this whole Python 3 problem was to a small extend caused by people rather having an incompatible syntax than two ways to print a string. Going through these troubles and then inflating the language with new syntax for the same thing seems like a waste.
- kbenson 10y agoIn that respect, don't list comprehensions fall under the same category of multiple ways to do something? They didn't exist in the language until 2.0[1], and it's just a convenient way to encode a couple for loops, is it not[2]? At some point you have to accept that it's a trade-off, and the more complex the topic, the less well served it will be by only one or two ways to do it. In this case, there were actually already three ways to do it, since you could always use plain string concatenation with `+`. If python were truly about one way to do it more than anything else, that would be the only way to build complex strings from variables, but since convenience is also important, numerous shortcuts were deemed acceptable and implemented. 1: https://en.wikipedia.org/wiki/History_of_Python#Version_2.0 https://en.wikipedia.org/wiki/History_of_Python#Version_2.0 2: And for my money, significantly more confusing (and less powerful) than something like Perl's `map`.
- allendoerfer 10y agoList comprehension is the one obvious way to do do a quick map, a for loop would be used for more complex stuff and side effects. A real map is more niche but probably necessary when doing eval stuff or stylisticly nicer in a module where you do several functional tasks. String concatenation has one use-case where it is the obvious way, too, namely when you have to concatenate already existing strings without modification, since "%s%s" % (a, b) is just stupid, beyond that you would use string formatting. Maybe there are unique obvious use-cases for the different formatting options as well, but I don't know them. I give you, that "%" is bad for named arguments, but why wouldn't you just improve that then? I think they do the exact same thing and are good for the exact same contexts. If somebody knows more than me, I would be very happy to be proven wrong.
- kbenson 10y ago> I give you, that "%" is bad for named arguments, but why wouldn't you just improve that then? Perhaps they should. I'm not making a case specifically for the proposed strategy (interpolation), just that if they are going to implement it, that it took this long is odd, and somewhat laughable. Personally, I think the named string format is superior to this new feature in many respects except the most simplistic of cases, but I don't often write python, so my opinion may be colored by that.
- kevin_thibedeau 10y agoThe format() method is more capable as it plays nice with heterogeneous duck-typed objects. It is type aware and you can supply your own custom formatters through __format__ methods that can receive parameters from the format string. % will spew TypeErrors if you don't get the format specifier right.