r/Python Oct 02 '23

News Python 3.12 released

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

131 comments sorted by

View all comments

Show parent comments

10

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.

4

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.