4 ms·
This was my first thought as well. But an important difference is that the arguments are not eagerly evaluated, but they are passed as lambdas which can be eval
by Epa095 2y ago
This was my first thought as well. But an important difference is that the arguments are not eagerly evaluated, but they are passed as lambdas which can be evaluated if desired. This means that it can be used for example in log messages (if you don't want to evaluate the string at the wrong log levels). But is it worth it for that? Idk.
- masklinn 2y agoEven if eager evaluation it's already a very compelling way of managing basically every lightweight "templating" for safety: e.g. embedded dynamic HTML or SQL. `markupsafe` is great, but it's way too easy to perform formatting before calling it, especially with f-strings. That f-strings were "static" was by far my biggest criticism of it, given how useful I find JS's template strings. And this proposal seems like a straight up better version of template strings: - the static strings and interpolations are not split and don't have to be awkwardly re-interpsersed which I've never found 100% trouble and 0% utility - the lazy evaluation means it can be used for things like logging (which really want lazy evaluation), or meta-programmation (because you can introspect the callables, and you get the expression text)
- codethief 2y ago> - the lazy evaluation means it can be used for things like logging (which really want lazy evaluation) Could you elaborate? I would find it rather surprising if my log messages don't contain the data at the very moment I invoke the logger.
- masklinn 2y agoThe expressions for the data you want to log out can be expensive, so ideally you only want to compute them after you’ve checked if the logger was enabled for the level you need. In most APIs this requires an explicit conditional check and the average developer will not think of it. This allows said check to be performed internally.
- skywhopper 2y agoYou can’t get log4j-style security crises with eager evaluation.
- deleted 2y ago[deleted]
- zelphirkalt 2y agoThere already is a syntax for writing log messages where the arguments are only evaluated when they are needed. logger.debug("bla: %s" % myvar).
- Hasnep 2y agoActually, the % syntax eagerly evaluates the log string, you need to pass the variables as arguments to the logging function like this: logger.debug("bla: %s", myvar). It's such a subtle difference that I only notice it when my IDE underlines it :/
- zelphirkalt 2y agoYou are right! I mixed them up as well. Here is a good source: https://blog.pilosus.org/posts/2020/01/24/python-f-strings-in-logging/ https://blog.pilosus.org/posts/2020/01/24/python-f-strings-i... (not mine)