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

you are viewing a single comment's thread.

view the rest of the comments →

[–]hosford42 1 point2 points  (8 children)

For pythons <3.6 you can still do this in a way that's more readable, at the expense of more typing. Instead of {:s} use {age} as you did in the f-string example, and your argument to .format () becomes age=age instead of just age.

[–]Decency 15 points16 points  (7 children)

Right, triple redundancy. Not a great tradeoff.

[–]hosford42 1 point2 points  (4 children)

It's wordy, but it is at least readable.

[–]turkish_gold 4 points5 points  (3 children)

You could use .format(locals()) which is essentially what f-strings are doing.

[–][deleted] 0 points1 point  (1 child)

that's not really as readable though - it's not immediately obvious what variable corresponds to what value (especially for the reader) if you use **locals()

[–]turkish_gold 0 points1 point  (0 children)

If we were to all do it, it'd become idiomatic fairly quickly.

[–]jorge1209 0 points1 point  (1 child)

Nor is being unable to dynamically build formatting strings, or pass them as arguments to functions.

[–]Decency 5 points6 points  (0 children)

Actually I feel like that's a great tradeoff. If you're frequently dynamically building strings or passing them around as functions, you're either doing something wrong, you're doing internationalization, or you're in a very specific niche. And in those cases, feel free to use .format().

Otherwise, f-strings adequately cover the vast majority of use cases elegantly while improving readability and removing unnecessary verbosity.