In the Python project I work on, some colleague likes to use the string's format method. Sometimes a bit too much for my taste. For instance, I can often see this kind of code:
Usually, I prefer to use an f-string:
But it could be a matter of taste. However, in some cases, I would prefer to use another approach.
For instance, in the case of the print method, there is already an existing pattern. I see often this kind of code:
I replace it usually with this code:
A bit more annoying is the use of format inside logging. I often see this code:
In case of logging, the pattern is here for a reason. If logging level is set to WARNING for instance, you want to avoid the string formatting, which takes some processing time. The preferred pattern is the following:
Finally, there are the cases where the use of format is completely insane. Here is an example:
Maybe my knowledge of Python is too limited, but value being already a string, is there any difference with this code:
No comments:
Post a Comment