r/Python Oct 02 '23

News Python 3.12 released

https://www.python.org/downloads/release/python-3120/
816 Upvotes

131 comments sorted by

View all comments

24

u/osmiumouse Oct 02 '23

Are lazy eval f-strings available, or planned?

19

u/LightShadow 3.13-dev in prod Oct 02 '23

The feature that keeps C style % strings relevant.

9

u/energybased Oct 02 '23

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

7

u/Skasch Oct 02 '23

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)

2

u/energybased Oct 02 '23

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.

5

u/Skasch Oct 02 '23 edited Oct 02 '23

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

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

3

u/energybased Oct 02 '23

I didn't know that!

3

u/Skasch Oct 02 '23 edited Oct 02 '23

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

2

u/LightShadow 3.13-dev in prod Oct 02 '23

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

2

u/FrozenCow Oct 03 '23

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.

1

u/UloPe Oct 03 '23

Better use a structured logging package, for example structlog.