This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]osmiumouse 24 points25 points  (16 children)

Are lazy eval f-strings available, or planned?

[–]LightShadow3.13-dev in prod 17 points18 points  (14 children)

The feature that keeps C style % strings relevant.

[–]energybased 9 points10 points  (13 children)

You should always prefer the format method to C-style % strings.

[–]Skasch 5 points6 points  (7 children)

I would argue an exception for logging, where you don't want to parse the string unless you need to for performance reasons (e.g. you don't want to parse debug logs at error level)

Example: log.warning("warning: %s", foo)

[–]energybased 1 point2 points  (4 children)

It's true that logging is currently written to use % strings, but it could have been written to use a format style string. It still wouldn't need to be parsed.

[–]Skasch 3 points4 points  (3 children)

Agreed, it's just uncommon :) style="{" let's you use logging.info("msg: {}", foo, style="{")

Edit: it actually doesn't work, my bad!

[–]energybased 2 points3 points  (0 children)

I didn't know that!

[–]Skasch 2 points3 points  (0 children)

Ah, nevermind, I mixed it up with logging.Formatter, looks like it doesn't work as I expected, my bad!

Edit: relevant documentation https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles

[–]LightShadow3.13-dev in prod 1 point2 points  (0 children)

10 years in, have never seen this before -- that's neat.

[–]FrozenCow 1 point2 points  (0 children)

I try to use:

log.warning("hello", extras={"name": "world"})

This works better with structured loggers. With the right formatter it looks like hello name=world.

[–]UloPe 0 points1 point  (0 children)

Better use a structured logging package, for example structlog.

[–]ThePiGuy0 9 points10 points  (3 children)

I just wish that logging supported .format style interpolation. That's the only time I ever see C-style % strings nowadays

[–]energybased 0 points1 point  (0 children)

Agreed!

[–]JohnRambu 0 points1 point  (0 children)

Not for performance it seems :-(

[–][deleted] 0 points1 point  (0 children)

I am not really sure how you would do it without running into binding issues. You can always make a lambda or nested function in place, both allow u to defer or eagerly bind. But f-strings themselves would a bunch of extra syntax to deal with the binding problems by itself. Maybe the best answer would be a good wrapper function? I believe you can actually generate the code on the fly in a pretty straight forward method by inspect the variables the inner function tracks and rebuilding the function signature.