4 ms·
Can anyone explain the creators' reasoning beyond the existing string interpolation in Python? I never thought it was cumbersome, on the contrary I liked the ve
by mcbetz 12y ago
Can anyone explain the creators' reasoning beyond the existing string interpolation in Python? I never thought it was cumbersome, on the contrary I liked the verbosity of "My {name} is, I am {years} old".format(name=name, years=years) and I think there are good reasons (beyond dragonwriter's reusability argument).
- aetherson 12y agoSo, like, just to point out, your example would be, with name Adam and age 10: "My Adam is, I am 10 old." Correcting your example: "My name is {name}, I am {years} years old".format(name=name, years=years) So to throw that out, that one line includes the word "name" four freaking times, and years four freaking times. You say you like the verbosity of it. Why? Would you like this format yet more? "My name is {name=name}, I am {years=years} years old".format(name=name, years=years) If not why not? It's yet more verbose. I think that the reason that other people like the non-verbose format of: "My name is {name}, I am {years} years old" # assuming the presence of local variables "name" and "years" Is that, well, it's pretty obvious what's going on here, and repeating name and years a bunch more times do not, it seems, make it any more clear what's going on. A reasonable argument might be that: "My name is {name}, I am {years} years old".format() Is more clear about what's going on. But repeating the variable names is not particularly elucidating.
- mcbetz 12y agoWell, I did not want to defend the way .format() works now. That's why I asked for the reasoning. Your reasonable argument sounds reasonable to me, I would certainly use it. But then my initial question is even more important: Why is this not the one Python way. What did the initial designers think?
- maxerickson 12y agoThe new style .format is talked about here: https://www.python.org/dev/peps/pep-3101/ https://www.python.org/dev/peps/pep-3101/ I guess finding discussion of the % formatting would be harder. I suspect that the discussion would have been about the tradeoff between the implicit variable insertion and simpler positional examples: "My name is %s, I am %s years old" % (name, years) (where there is definitely at least a tendency to avoid implicit behavior in the design of python; of course positional formatting like that is implicit, but it is quite a bit less implicit than automatically pulling variables out of the current scope) edit: the % formatting was probably informed by sprintf.
- mcbetz 12y agoThanks for the explanation and link!