From 53cbdfda37dbda819f18e6e2b8eddc2ef420c6e9 Mon Sep 17 00:00:00 2001 From: blaisep Date: Fri, 6 Sep 2019 11:52:52 -0700 Subject: [PATCH] Add a reference to f-strings to address issue #41 --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4431bc4..56ff0e5 100644 --- a/README.md +++ b/README.md @@ -534,7 +534,7 @@ But seriously: beautiful code without tests is simply worse than even the uglies This is a section for arguments we'd rather not settle. Don't rewrite other people's code because of this stuff. Feel free to use these forms interchangeably. -### `str.format` vs overloaded format `%` +### `str.format` vs overloaded format `%` vs f-strings `str.format` is more robust, yet `%` with `"%s %s"` printf-style strings is more concise. Both will be around forever. @@ -556,6 +556,9 @@ But use whichever one you please. We choose not to care. [str-format]: https://docs.python.org/2/library/string.html#formatspec +F-strings are [new in Python 3.6](https://docs.python.org/3/reference/lexical_analysis.html#f-strings) You may find them easier to compose and to read `f"result: {value:{width}.{precision}}"`. The tradeoffs are [localization](https://stackoverflow.com/a/56264202/5181298) and [security](http://lucumr.pocoo.org/2016/12/29/careful-with-str-format/). Python executes f-strings, so they present an additional attack surface: avoid including user-generated data. + + ### `if item` vs `if item is not None` This is unrelated to the earlier rule on `==` vs `is` for `None`. In this case, we are actually taking advantage of Python's "truthiness rules" to our benefit in `if item`, e.g. as a shorthand "item is not None or empty string."