The new year has arrived, and January brought a flurry of new and interesting Python enhancement proposals (PEPs). Topics range from f-string formalization and no-GIL Python to packaging. There’s been ample discussion on the Python Discourse forum about the implications of these PEPs, and if you’re still a bit wary of diving deeper into the discussions, then you can get a softer introduction here.
There have also been a couple of noteworthy new releases, first and foremost the fourth alpha release of Python 3.12. And finally, there’s a new job posting out for a security developer in residence sponsored by the Python Software Foundation (PSF).
Let’s dive into the biggest Python news from the past month!
Join Now:Click here to join the Real Python Newsletter and you'll never miss another Python tutorial, course update, or post.
PEP 701 Attempts to Formalize f-Strings
Python’s f-strings are great, but there are currently a few edge cases that might make you scratch your head.
For example, maybe you wanted to greet the new year with a carefully constructed f-string, but you ran into unexpected troubles:
>>> f"happy {"\n".join(["twenty","three","🥳"])#it's 2023!}!!"File "<stdin>", line 1 f"happy {"\n".join(["twenty", "three", "🥳"]) # it's 2023!}!!" ^SyntaxError: unexpected character after line continuation characterYou used some double quotes (") inside the curly braces ({}). It looks like Python isn’t happy with that and considers the f-string closed, even though the quotes appear inside the f-string expression part.
The new year is still fresh, so you won’t let this get you down! You just change the double quotes to single quotes:
>>> f"Happy {'\n'.join(['twenty','three','🥳'])#Yes!2023!}!!"File "<stdin>", line 1 f"Happy {'\n'.join(['twenty', 'three', '🥳']) # Yes! 2023!}!!" ^SyntaxError: f-string expression part cannot include a backslashOh! There’s another issue with your new year’s greeting f-string. It looks like you can’t include the backslash (\) character in an f-string expression. Well, maybe 2023 is more about expanding in breadth than in depth, you think to yourself. So you replace the newline character (\n) with a whitespace character (' ') and try again:
>>> f"Happy {' '.join(['twenty','three','🥳'])#Yes!2023!}!!"File "<stdin>", line 1 f"Happy {' '.join(['twenty', 'three', '🥳']) # Yes! 2023!}!!" ^SyntaxError: f-string expression part cannot include '#'Argh! Yet another problem! It seems like you can’t include comments in the f-string expression part—not even when they’re full of joy and optimism!
Python 3.6 introduced f-strings, and the LL(1) parser that Python used back then wasn’t able to handle these edge cases.
Maybe running into the limitations of f-strings has slightly dampened your optimistic beginning of 2023. But don’t write this year off quite yet, because these limitations may go away soon.
Pablo Galindo Salgado authored PEP 701 – Syntactic formalization of f-strings at the end of last year. The PEP proposes a formalized grammar for f-strings, which could be directly integrated into the parsing expression grammar (PEG) parser that Python’s been using since version 3.9.
Read the full article at https://realpython.com/python-news-january-2023/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]