5 ms·
The awkwardness of escaping makes me wonder what some vehemently pro f string people are smoking. .format is a neat, clear, terse way of constructing strings li
by pydry 7y ago
The awkwardness of escaping makes me wonder what some vehemently pro f string people are smoking. .format is a neat, clear, terse way of constructing strings like so:
description_string = "The {} is a problem, please look at: {}".format(
some_function(value)[lookup],
' '.join([item for item in list_of_items]),
)
Where, with f strings, you'd need to do some nasty escaping or create descriptive labels for the substitutions that were previously unnecessary.
With the second option you also get the problem of a potential loss of cohesion as the variable 'name = some_function(value)[lookup]' won't be intrinsically tied to the string where it is being used and tends to float away from the string where it's actually being used. e.g.
name = some_function(value)[lookup]
[ 50 lines of code ]
description_string = f"{name} is a problem..." etc.
Which creates additional problems with readability, bugs, etc.
- bocklund 7y agoYou can actually put those expressions in your format() into the f-string braces, no escaping required.
- Mister_Snuggles 7y agoYour first example works fine as an f-string without resorting to escaping, etc: >>> def some_function(): ... return ['a','b','c'] ... >>> list_of_items = ['one','two','three'] >>> s = f"The {some_function()[1]} is a problem, please look at: {' '.join([item for item in list_of_items])}" >>> s 'The b is a problem, please look at: one two three' >>> I'll admit that it's somewhat ugly, but there's no escaping needed.
- BerislavLopac 7y agoIn this particular example it gets even simpler: s = f"The {some_function()[1]} is a problem, please look at: {' '.join(list_of_items)}"
- pydry 7y agoYep and then as soon as you add quotation marks it breaks down: s = f"The {some_function()["WHOOPS!!!"]} is a problem, please look at: {' '.join([item for item in functino_call("whoops again")])}" This indirectly proves the point I think.
- dragonwriter 7y ago> Where, with f strings, you'd need to do some nasty escaping or create descriptive labels for the substitutions that were previously unnecessary. No, with f-strings, you'd have to take the expressions you use in the call to format and stick them between the curly braces, and trade the .format(...) after the string for an f before it. f-strings are almost without exception more terse than .format for single-use formatted strings.
- pydry 7y agoIt would be shorter if you stuck those expressions in between the curly braces, and it would break as soon as quotation marks are introduced. I guess it's a you don't use those too often in your expressions? If would be longer if you started attaching descriptive names to those expressions and using them in an f string.
- BerislavLopac 7y agoAs demonstrated, the example above works quite well with f-strings. However, I agree with you that there are some cases where .format() is a much better solution, which is when the variable values are provided dynamically. For example: aa = [ {"foo": "baz", "bar: "bam"}, {"foo": "blah", "bar": 1234}, ] for a in aa: print("This is my message which says {foo} and {bar}".format(**a))
- pydry 7y agoIt was demonstrated that it works quite well right up until the point where you start using quotation marks - " and/or ' in your code. Then it breaks.
- dragonwriter 7y agoActually, it works if you are using single or double quotes in your code, as long as you don't need both single and double quotes in the embedded expressions. And even that can be accommodated with f-here-strings.
- pydry 7y agoi.e. it becomes ugly and annoying to manage.
- BerislavLopac 7y ago"Ugly" is highly subjective.