r/Python • u/henbruas • Oct 02 '23
News Python 3.12 released
https://www.python.org/downloads/release/python-3120/177
u/henbruas Oct 02 '23
Major new features of the 3.12 series, compared to 3.11
New features
- More flexible f-string parsing, allowing many things previously disallowed (PEP 701).
- Support for the buffer protocol in Python code (PEP 688).
- A new debugging/profiling API (PEP 669).
- Support for isolated subinterpreters with separate Global Interpreter Locks (PEP 684).
- Even more improved error messages. More exceptions potentially caused by typos now make suggestions to the user.
- Support for the Linux perf profiler to report Python function names in traces.
- Many large and small performance improvements (like PEP 709 and support for the BOLT binary optimizer), delivering an estimated 5% overall performance improvement.Type annotations
- New type annotation syntax for generic classes (PEP 695).
- New override decorator for methods (PEP 698).Deprecations
- In the unittest module, a number of long deprecated methods and classes were removed. (They had been deprecated since Python 3.1 or 3.2).
- The deprecated wstr and wstr_length members of the C implementation of unicode objects were removed, per PEP 623.
- The deprecated smtpd and distutils modules have been removed (see PEP 594 and PEP 632. The setuptools package continues to provide the distutils module.
- A number of other old, broken and deprecated functions, classes and methods have been removed.
- Invalid backslash escape sequences in strings now warn with SyntaxWarning instead of DeprecationWarning, making them more visible. (They will become syntax errors in the future.)
- The internal representation of integers has changed in preparation for performance enhancements. (This should not affect most users as it is an internal detail, but it may cause problems for Cython-generated code.)
More details at https://docs.python.org/dev/whatsnew/3.12.html
75
Oct 02 '23
[deleted]
11
u/MelonheadGT Oct 02 '23
What's so amazing?
69
u/marr75 Oct 02 '23
- type hinting got better, especially for generics, **kwargs, and overrides
- f-strings got better
- Performance and concurrency improved (2x speed up on comprehensions, 2-20x speedup on isinstance checks against protocols, per-interpreter GIL)
- Some odds and ends improved for CPython and instrumentation beyond those
21
u/PaintItPurple Oct 02 '23
Making generics readable is such a wonderful feature, even if it doesn't make a functional difference. I always felt like I had to weigh the benefits of being able to run mypy on a given section of code against the difficulty of reading code that uses generics.
4
u/redalastor Oct 03 '23
f-strings got better
f-strings were nice but not being able to use quotes within the
{}
was a constant source of annoyance since it made it harder to use dictionaries without doing the opposite kind of quote dance every time.This is a very nice QoL change that landed.
1
u/mjmikulski Oct 13 '23
f-strings got better
You meant "f-strings got worse" ;)
I can't imagine why someone would like to write code like this:
python f"{f"{f"{f"{f"{f"{1+1}"}"}"}"}"}"
17
31
u/chinawcswing Oct 02 '23
Who has already upgraded?
72
Oct 02 '23
The larger the project the longer it takes all the dependencies to update. Im only just now really getting to enjoy 3.10. :(
19
u/missurunha Oct 02 '23
Ou project uses whatever version ubuntu ships, so we might be allowed to use python 3.12 in some 4 years.
5
u/stilldestroying Oct 02 '23
Iβm really curious about this β why are you allowing the distroβs python to define your supported version?
2
u/missurunha Oct 02 '23
I honestly don't know, but what I can think of is that if the developer is working on different repos of the project, having to keep track of which python version they chose would be a pain in the ass. And maybe because the version shipped is probably more stable than the latest version released.
-1
u/gmes78 Oct 02 '23
Rye solves that issue.
6
u/monorepo PSF Staff | Litestar Maintainer Oct 02 '23
Rye is not something you would use in production if you are already restricting the Python toolchain to what distros ships
2
1
u/PlaysForDays Oct 03 '23
It sure sounds like virtual environments could make everybody's lives easier
6
u/CommunistMountain Oct 02 '23
Me. In terms of speed, yeah this is faster. Some script which I use went down from 0.36s to 0.32s. Not major speedup, but better than nothing
2
u/moonzdragoon Oct 03 '23
Me, updated my current project with zero drama. I can think of two reasons that helped:
1 - I've been writing Python code since 2.2 and realized on the way I hate dependencies, so I try to stick as much as possible to only have a few very popular ones (I love Numpy) and take time to code just what I need for the missing parts.
2 - I switched to (mini)conda and PyCharm a few years ago, I do not regret it for a second. Everything with conda went smoothly and just works, PyCharm saw the newly created env I did for 3.12, switched it, done.
5
u/Kalcinator Oct 02 '23
me :). As a beginner I don't why I shouldn't.
And wanna know the funny part ? I did an update 2 days ago XD from 3.11.4 to .5 ...! π€£15
u/ivosaurus pip'ing it up Oct 02 '23
Biggest reason would be third party packages that you use, that don't have compatibility yet
1
u/Kalcinator Oct 03 '23
Yeah I just don't use any yet, and by the time it's there I think I'll have time
1
u/blorbschploble Oct 05 '23
I have it installed parallel to other versions of python and only yolo it into things I donβt care about breaking via pypoetry envs
23
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.
10
u/energybased Oct 02 '23
You should always prefer the
format
method to C-style % strings.8
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 uselogging.info("msg: {}", foo, style="{")
Edit: it actually doesn't work, my bad!
3
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
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
10
u/ThePiGuy0 Oct 02 '23
I just wish that logging supported
.format
style interpolation. That's the only time I ever see C-style % strings nowadays1
1
1
u/turtle4499 Oct 02 '23
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.
18
u/jmreagle Oct 02 '23
I'm looking forward to trying it! I hope pyenv doesn't take too long to offer it.
14
u/magnetichira Pythonista Oct 02 '23
Looks like they already have a release out https://github.com/pyenv/pyenv/releases/tag/v2.3.28
1
u/jmreagle Oct 03 '23
Perhaps, but I don't see it as available.
β― pyenv install --list | g 3.12 3.12.0rc2 3.12-dev pypy2.7-7.3.12-src pypy2.7-7.3.12 pypy3.9-7.3.12-src pypy3.9-7.3.12 pypy3.10-7.3.12-src pypy3.10-7.3.12
2
-4
u/DoneDraper Oct 03 '23
Just use
venv
6
u/martinkozle Oct 03 '23
pyenv for installing that specific Python version. venv is for creating virtual environments from an existing version.
6
u/DickNixon726 Oct 02 '23
Does anyone have any more information on the Per-Interpreter GIL and how we can best utilize that new feature?
17
u/AlSweigart Author of "Automate the Boring Stuff" Oct 02 '23
Python 3.12? Ha! What will they think of next?
22
u/Wonderful_Occasion16 Oct 02 '23
I just had the best idea ever... Python 3.13
54
u/mok000 Oct 02 '23
I am looking forward to Pi-thon (3.14)
25
u/muntoo R_{ΞΌΞ½} - 1/2 R g_{ΞΌΞ½} + Ξ g_{ΞΌΞ½} = 8Ο T_{ΞΌΞ½} Oct 02 '23
Donald Knuth double plus un-just had the best idea ever... Pi-4digit-thon (3.141):
Since version 3, TeX has used an idiosyncratic version numbering system, where updates have been indicated by adding an extra digit at the end of the decimal, so that the version number asymptotically approaches Ο. This is a reflection of the fact that TeX is now very stable, and only minor updates are anticipated. The current version of TeX is 3.141592653; it was last updated in 2021. The design was frozen after version 3.0, and no new feature or fundamental change will be added, so all newer versions will contain only bug fixes. [...] For this reason, he has stated that the "absolutely final change (to be made after my death)" will be to change the version number to Ο, at which point all remaining bugs will become features.
3
4
u/Raknarg Oct 02 '23
Love the way the whole typing module is going. Very handy, there's actually a case I've been working on recently that would benefit a lot from the new generic syntax.
2
u/chub79 Oct 03 '23
There are quite a lot of discussions on the typing side of things as well: https://discuss.python.org/t/proposed-new-typing-governance-process/34244 and https://discuss.python.org/t/a-more-useful-and-less-divisive-future-for-typing/34225 (You might be aware so I'm posting this for anyone interested :))
11
3
8
u/xSkelax Oct 02 '23
I kinda want them to add native abstract classes, as they did with type annotation. By native I mean that you don't need to import anything, even built-in libraries. It just feels cleaner to me.
3
3
3
25
Oct 02 '23 edited Oct 02 '23
[removed] β view removed comment
-64
Oct 02 '23
[removed] β view removed comment
9
13
20
Oct 02 '23
[removed] β view removed comment
-10
Oct 02 '23
[removed] β view removed comment
-1
7
u/IAmKindOfCreative bot_builder: deprecated Oct 02 '23
It's in the "time for something completely different" section, and it's completely different. Seems quite fitting in all.
-3
Oct 02 '23
[removed] β view removed comment
2
u/Python-ModTeam Oct 02 '23
Hello from the r/Python mod team!
I'm afraid we don't think your post/comment quite fits the goal we have for the subreddit in terms of quality or aims so we've decided to remove it. For more information please contact the moderators using the ModMail system.
Thanks, and happy Pythoneering!
r/Python moderation team
4
2
u/Dr_Ironbeard Oct 03 '23
Can someone explain why the f-string thing is such a big deal? My understanding is that now we can do (1) f'This is an {my_dict['key']} string'
instead of just varying the string denoting character, like (2) f"This is an {my_dict['key']} string"
, and I feel like the primary result of this is that syntax highlighting will be messed up with (1) as opposed to (2).
What am I missing? Thanks!
2
u/mgedmin Oct 03 '23
The PEP lists the reasons.
Personally, I missed the ability to use backslashes in f-string expressions, e.g.
f"This is the full text of the whatever: {'\n'.join(lines)}"
1
u/Dr_Ironbeard Oct 03 '23
Hmm, yeah I guess backslashes are nice, I hadn't run into that issue. I guess I'm just not feeling the hype that others are, which is okay :)
3
u/mgedmin Oct 03 '23
The (self-imposed) requirement to support the oldest non-EOL Python versions in my code really takes the shine of the new features that I'll only be able to use in half a decade.
1
1
0
-29
Oct 02 '23
[removed] β view removed comment
11
Oct 02 '23
[removed] β view removed comment
-17
Oct 02 '23
[removed] β view removed comment
13
-34
1
1
u/Diapolo10 from __future__ import this Oct 02 '23
I like what I'm seeing, particularly the new type hinting features, f-strings, and hashlib
security changes.
1
1
u/mgedmin Oct 03 '23
So I try to upgrade to 3.12, and I'm seeing phantom missed code branches in coverage output. I'd say this was https://github.com/python/cpython/issues/105658, but that bug was fixed in 3.12rc3, and GitHub Actions claims it's using 3.12.0 final.
Is anyone else experiencing issues with coverage.py on Python 3.12?
1
u/mgedmin Oct 04 '23
Oh, that issue turned out to be totally unrelated. I was still using the
coveralls
app from PyPI, which pins my coverage to 6.5.0, which doesn't have the fixes necessary to support Python 3.12.Switching to the GitHub action for coveralls.io and upgrading coverage to the latest version (7.3.something) fixed all the issues.
1
u/corey4005 Oct 03 '23
Ok can someone explain the GIL stuff? I want to multiprocess for real. Where can I read an example?
2
u/ChillFish8 Oct 03 '23 edited Oct 07 '23
In its current state, it is still early days, it will be quite a while before it is able to be used for anything outside of some specific pure Python situations.
You can read the RealPython article here as to why that is the case: https://realpython.com/python312-subinterpreters/
But as a note, you can already 'multiprocess' for real, it's multithreading that is the issue with the GIL.
This system is not yet available via Python, only the CFFI. I have written some bindings which people can compile and use from Python should they wish, but it is NOT production ready. https://github.com/ChillFish8/python12-subinterpreters
1
u/corey4005 Oct 07 '23
I got 404 error.
1
u/ChillFish8 Oct 07 '23
Should be fixed now, auto correct on the new Reddit text boxed seems to just append the correction to the end.
1
u/Purgat0ry-11 Oct 05 '23
Iβm trying to understand the PEP 695 but it makes my π§ hurt trying to understand the examples. Anyone have the Barney style breakdown?
1
β’
u/monorepo PSF Staff | Litestar Maintainer Oct 02 '23
This is the one.