4 ms·
As I understand, it may help a bit with logging performance, not sure, still trying to understand the template abilities. So, right now, you have two options t
by leobuskin 1y ago
As I understand, it may help a bit with logging performance, not sure, still trying to understand the template abilities.
So, right now, you have two options to log:
1. `logger.debug(f'Processing {x}')` - looks great, but evaluates anyway, even if logging level > `logging.DEBUG`;
2. `logger.debug('Processing %s', x)` - won't evaluate till necessary.
What would be the approach with t-strings in this case? Would we get any benefits?
- davepeck 1y agoT-strings, like f-strings, are eagerly evaluated -- so in this sense, no, there's no benefit.
- trashburger 1y agoNot quite; the interpolations are not eagerly stringified which is the potentially expensive part. In this sense it's kind of a middle ground between the two approaches.
- davepeck 1y agoSure, good point; I generally think of evaluation, not stringification, as the likely dominant expense. But maybe it’s sometimes the other way around?
- bcoates 1y agoThe expression (x) is eagerly evaluated in both cases, cuz that's how Python works. You can defer the format call but Python fundamentally doesn't have an equivalent of lazy/compile time flag argument evaluation and this doesn't change that. For a logger t-strings are mostly just a more pleasant and less bug-prone syntax for #2