ufal / npfl122

NPFL122 repository
Creative Commons Attribution Share Alike 4.0 International
13 stars 23 forks source link

Using f-string instead of `str.format()` #72

Closed Rattko closed 2 years ago

Rattko commented 2 years ago

Hello, I would like to propose an enhancement to the current set of templates. I believe, it would be worth the effort to start using f-strings, a feature available since Python 3.6. It would improve readability of the code and it would make the code shorter and more concise.

I would be willing to create a pull request for all of the already published assignments which would implement this transition to f-strings.

foxik commented 2 years ago

Hi,

thanks for the suggestion!

Personally I am not a big fan on f-strings. I can see the appeal when you want to print the values of variables, i.e.,

print(f"Test accuracy: {test_accuracy}")

but many times you are printing more complex expressions, and sometimes I find it difficult to draw a line. I.e., would you write

f"{100 * test_accuracy:.2f}%"
f"{np.mean(rewards)}"

or not? Personally I prefer str.format in both such cases.

In my head, the f-strings blur the distinction between the "template" for the message itself and the data you want to print, i.e., the code is then intermixed with the texts (and from localization point of view that should not happen); of course when you use just variable names, that is fine, but in many cases I need more than just take a variable.

Looking at the str.format we have currently in the repository, I am actually satisfied with their usage, so I do not want to pursue this direction. It should be considered my personal taste though, I am not saying f-strings are bad or anything ;-)

Cheers.