diff options
author | Carlton Gibson <carlton.gibson@noumenal.es> | 2020-07-21 08:45:09 +0200 |
---|---|---|
committer | Carlton Gibson <carlton.gibson@noumenal.es> | 2020-10-14 13:21:10 +0200 |
commit | 05dbb4703622908316603d83321292bf9d22c157 (patch) | |
tree | 0ce54e122abb58d61c86f2d4115c9a876e9b75ef | |
parent | ac6c4260074de43a978e5c6553ef89441e1d6748 (diff) | |
download | django-c/29988-allow-f-strings.tar.gz |
Fixed #29988 -- Updated coding style to allow f-strings.c/29988-allow-f-strings
Thanks to Nick Pope for review.
-rw-r--r-- | docs/internals/contributing/writing-code/coding-style.txt | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/docs/internals/contributing/writing-code/coding-style.txt b/docs/internals/contributing/writing-code/coding-style.txt index 56f6bd5738..5f5c42ae76 100644 --- a/docs/internals/contributing/writing-code/coding-style.txt +++ b/docs/internals/contributing/writing-code/coding-style.txt @@ -52,6 +52,41 @@ Python style single quote. Don't waste time doing unrelated refactoring of existing code to conform to this style. +* String variable interpolation may use `%-formatting`__, `f-strings`__, or + :py:meth:`str.format` as appropriate, with the goal of maximizing code + readability. + + Final judgments of readability are left to the Merger's discretion. As a + guide, f-strings should use only plain variable and property access, with + prior local variable assignment for more complex cases:: + + # Allowed + f'hello {user}' + f'hello {user.name}' + f'hello {self.user.name}' + + # Disallowed + f'hello {get_user()}' + f'you are {user.age * 365.25} days old' + + # Allowed with local variable assignment + user = get_user() + f'hello {user}' + user_days_old = user.age * 365.25 + f'you are {user_days_old} days old' + + f-strings should not be used for any string that may require translation, + including error and logging messages. + + In general ``format()`` is more verbose, so the other formatting methods are + preferred. + + Don't waste time doing unrelated refactoring of existing code to adjust the + formatting method. + + __ https://docs.python.org/3/library/stdtypes.html#old-string-formatting + __ https://docs.python.org/3/reference/lexical_analysis.html#f-strings + * Avoid use of "we" in comments, e.g. "Loop over" rather than "We loop over". * Use underscores, not camelCase, for variable, function and method names |