Text of docs.python.org › pa › 3 › whatsnew › 3.12.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

What's New In Python 3.12 — Python 3.15.2 documentation

Navigation index modules | next | previous | Python » 3.15.2 documentation » ਪਾਈਥਨ ਵਿੱਚ ਨਵਾਂ ਕੀ ਹੈ » What's New In Python 3.12 | Theme | What's New In Python 3.12 ¶ Editor : Adam Turner This article explains the new features in Python 3.12, compared to 3.11. Python 3.12 was released on October 2, 2023. For full details, see the changelog . See also PEP 693 -- Python 3.12 Release Schedule Summary -- Release highlights ¶ Python 3.12 is a stable release of the Python programming language, with a mix of changes to the language and the standard library.

The library changes focus on cleaning up deprecated APIs, usability, and correctness. Of note, the distutils package has been removed from the standard library. Filesystem support in os and pathlib has seen a number of improvements, and several modules have better performance. The language changes focus on usability, as f-strings have had many limitations removed and 'Did you mean ...' suggestions continue to improve.

The new type parameter syntax and type statement improve ergonomics for using generic types and type aliases with static type checkers. This article doesn't attempt to provide a complete specification of all new features, but instead gives a convenient overview. For full details, you should refer to the documentation, such as the Library Reference and Language Reference . If you want to understand the complete implementation and design rationale for a change, refer to the PEP for a particular new feature; but note that PEPs usually are not kept up-to-date once a feature has been fully implemented.

New syntax features: PEP 695 , type parameter syntax and the type statement New grammar features: PEP 701 , f-strings in the grammar Interpreter improvements: PEP 684 , a unique per-interpreter GIL PEP 669 , low impact monitoring Improved 'Did you mean ...' suggestions for NameError , ImportError , and SyntaxError exceptions Python data model improvements: PEP 688 , using the buffer protocol from Python Significant improvements in the standard library: The pathlib.Path class now supports subclassing The os module received several improvements for Windows support A command-line interface has been added to the sqlite3 module isinstance() checks against runtime-checkable protocols enjoy a speed up of between two and 20 times The asyncio package has had a number of performance improvements, with some benchmarks showing a 75% speed up.

A command-line interface has been added to the uuid module Due to the changes in PEP 701 , producing tokens via the tokenize module is up to 64% faster. Security improvements: Replace the builtin hashlib implementations of SHA1, SHA3, SHA2-384, SHA2-512, and MD5 with formally verified code from the HACL* project. These builtin implementations remain as fallbacks that are only used when OpenSSL does not provide them. C API improvements: PEP 689 , unstable C API tier PEP 683 , immortal objects CPython implementation improvements: PEP 709 , comprehension inlining CPython support for the Linux perf profiler Implement stack overflow protection on supported platforms New typing features: PEP 692 , using TypedDict to annotate **kwargs PEP 698 , @typing.override decorator Important deprecations, removals or restrictions: PEP 623 : Remove wstr from Unicode objects in Python's C API, reducing the size of every str object by at least 8 bytes.

PEP 632 : Remove the distutils package. See the migration guide for advice replacing the APIs it provided. The third-party Setuptools package continues to provide distutils , if you still require it in Python 3.12 and beyond. gh-95299 : Do not pre-install setuptools in virtual environments created with venv . This means that distutils , setuptools , pkg_resources , and easy_install will no longer available by default; to access these run pip install setuptools in the activated virtual environment.

The asynchat , asyncore , and imp modules have been removed, along with several unittest.TestCase method aliases . New Features ¶ PEP 695: Type Parameter Syntax ¶ Generic classes and functions under PEP 484 were declared using a verbose syntax that left the scope of type parameters unclear and required explicit declarations of variance. PEP 695 introduces a new, more compact and explicit way to create generic classes and functions : def max [ T ]( args : Iterable [ T ]) -> T : ... class list [ T ]: def __getitem__ ( self , index : int , / ) -> T : ... def append ( self , element : T ) -> None : ...

In addition, the PEP introduces a new way to declare type aliases using the type statement, which creates an instance of TypeAliasType : type Point = tuple [ float , float ] Type aliases can also be generic : type Point [ T ] = tuple [ T , T ] The new syntax allows declaring TypeVarTuple and ParamSpec parameters, as well as TypeVar parameters with bounds or constraints: type IntFunc [ ** P ] = Callable [ P , int ] # ParamSpec type LabeledTuple [ * Ts ] = tuple [ str , * Ts ] # TypeVarTuple type HashableSequence [ T : Hashable ] = Sequence [ T ] # TypeVar with bound type IntOrStrSequence [ T : ( int , str )] = Sequence [ T ] # TypeVar with constraints The value of type aliases and the bound and constraints of type variables created through this syntax are evaluated only on demand (see lazy evaluation ).

This means type aliases are able to refer to other types defined later in the file. Type parameters declared through a type parameter list are visible within the scope of the declaration and any nested scopes, but not in the outer scope. For example, they can be used in the type annotations for the methods of a generic class or in the class body. However, they cannot be used in the module scope after the class is defined.

See Type parameter lists for a detailed description of the runtime semantics of type parameters. In order to support these scoping semantics, a new kind of scope is introduced, the annotation scope . Annotation scopes behave for the most part like function scopes, but interact differently with enclosing class scopes. In Python 3.13, annotations will also be evaluated in annotation scopes. See PEP 695 for more details. (PEP written by Eric Traut.

Implementation by Jelle Zijlstra, Eric Traut, and others in gh-103764 .) PEP 701: Syntactic formalization of f-strings ¶ PEP 701 lifts some restrictions on the usage of f-strings . Expression components inside f-strings can now be any valid Python expression, including strings reusing the same quote as the containing f-string, multi-line expressions, comments, backslashes, and unicode escape sequences. Let's cover these in detail: Quote reuse: in Python 3.11, reusing the same quotes as the enclosing f-string raises a SyntaxError , forcing the user to either use other available quotes (like using double quotes or triple quotes if the f-string uses single quotes).

In Python 3.12, you can now do things like this: >>> songs = [ 'Take me back to Eden' , 'Alkaline' , 'Ascensionism' ] >>> f "This is the playlist: { ", " . join ( songs ) } " 'This is the playlist: Take me back to Eden, Alkaline, Ascensionism' Note that before this change there was no explicit limit in how f-strings can be nested, but the fact that string quotes cannot be reused inside the expression component of f-strings made it impossible to nest f-strings arbitrarily.

In fact, this is the most nested f-string that could be written: >>> f """ { f ''' { f ' { f " { 1 + 1 } " } ' } ''' } """ '2' As now f-strings can contain any valid Python expression inside expression components, it is now possible to nest f-strings arbitrarily: >>> f " { f " { f " { f " { f " { f " { 1 + 1 } " } " } " } " } " } " '2' Multi-line expressions and comments: In Python 3.11, f-string expressions must be defined in a single line, even if the expression within the f-string could normally span multiple lines (like literal lists being defined over multiple lines), making them harder to read.

In Python 3.12 you can now defin

The rest of this page is on the live site.