3 ms·
I guess, by %-syntax, you mean templace strings, something like logging.info('logging object %s', obj)? (as opposed to the 'old' printf-style formatting with %
by karlicoss 6y ago
I guess, by %-syntax, you mean templace strings, something like logging.info('logging object %s', obj)? (as opposed to the 'old' printf-style formatting with % operator)
This is a fair concern, but in my personal experience, that was never an issue in Python. Sometimes I had similar problems in c++ code, but then typically if you choose c++, it's probably for the very reason you expect to squeeze every last bit of performance.
Another potential benefit of pattern strings is that they allow for more defensive behaviour
- e.g. imagine __str__ method for obj throws the extension, then f'logging object {obj}' will crash immediately. logging.info('object %s', obj) can catch the exception from __str__, log it, and continue
- or imagine a typo in the logging pattern, something like logging.info('objects %s %s', obj1). Depending on the logger configuration, it might just log the error (e.g. 'missing value') and carry on without crashing.
Such defensive behaviour may or may not be desirable, of course. In my experience, such errors just never happen (especially the latter, if you use f-strings and mypy, most of these can be statically checked). But YMMV