by pydry · 6y ago · 0 comments
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. >>> 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. 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. s = f"The {some_function()[1]} is a problem, please look at: {' '.join(list_of_items)}" 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))Then it breaks.
And even that can be accommodated with f-here-strings.
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.
If would be longer if you started attaching descriptive names to those expressions and using them in an f string.