What's New In Python 3.11 — Python 3.15.2 documentation
Navigation index modules | next | previous | Python » 3.15.2 documentation » ਪਾਈਥਨ ਵਿੱਚ ਨਵਾਂ ਕੀ ਹੈ » What's New In Python 3.11 | Theme | What's New In Python 3.11 ¶ Editor : Pablo Galindo Salgado This article explains the new features in Python 3.11, compared to 3.10. Python 3.11 was released on October 24, 2022. For full details, see the changelog . Summary -- Release highlights ¶ Python 3.11 is between 10-60% faster than Python 3.10.
On average, we measured a 1.25x speedup on the standard benchmark suite. See Faster CPython for details. New syntax features: PEP 654: Exception Groups and except* New built-in features: PEP 678: Exceptions can be enriched with notes New standard library modules: PEP 680 : tomllib — Support for parsing TOML in the Standard Library Interpreter improvements: PEP 657: Fine-grained error locations in tracebacks New -P command line option and PYTHONSAFEPATH environment variable to disable automatically prepending potentially unsafe paths to sys.path New typing features: PEP 646: Variadic generics PEP 655: Marking individual TypedDict items as required or not-required PEP 673: Self type PEP 675: Arbitrary literal string type PEP 681: Data class transforms Important deprecations, removals and restrictions: PEP 594 : Many legacy standard library modules have been deprecated and will be removed in Python 3.13 PEP 624 : Py_UNICODE encoder APIs have been removed PEP 670 : Macros converted to static inline functions New Features ¶ PEP 657: Fine-grained error locations in tracebacks ¶ When printing tracebacks, the interpreter will now point to the exact expression that caused the error, instead of just the line.
For example: Traceback ( most recent call last ): File "distance.py" , line 11 , in < module > print ( manhattan_distance ( p1 , p2 )) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "distance.py" , line 6 , in manhattan_distance return abs ( point_1 . x - point_2 . x ) + abs ( point_1 . y - point_2 . y ) ^^^^^^^^^ AttributeError : 'NoneType' object has no attribute 'x' Previous versions of the interpreter would point to just the line, making it ambiguous which object was None .
These enhanced errors can also be helpful when dealing with deeply nested dict objects and multiple function calls: Traceback ( most recent call last ): File "query.py" , line 37 , in < module > magic_arithmetic ( 'foo' ) File "query.py" , line 18 , in magic_arithmetic return add_counts ( x ) / 25 ^^^^^^^^^^^^^ File "query.py" , line 24 , in add_counts return 25 + query_user ( user1 ) + query_user ( user2 ) ^^^^^^^^^^^^^^^^^ File "query.py" , line 32 , in query_user return 1 + query_count ( db , response [ 'a' ][ 'b' ][ 'c' ][ 'user' ], retry = True ) ~~~~~~~~~~~~~~~~~~^^^^^ TypeError : 'NoneType' object is not subscriptable As well as complex arithmetic expressions: Traceback ( most recent call last ): File "calculation.py" , line 54 , in < module > result = ( x / y / z ) * ( a / b / c ) ~~~~~~^~~ ZeroDivisionError : division by zero Additionally, the information used by the enhanced traceback feature is made available via a general API, that can be used to correlate bytecode instructions with source code location.
This information can be retrieved using: The codeobject.co_positions() method in Python. The PyCode_Addr2Location() function in the C API. See PEP 657 for more details. (Contributed by Pablo Galindo, Batuhan Taskaya and Ammar Askar in bpo-43950 .) Note This feature requires storing column positions in Code Objects , which may result in a small increase in interpreter memory usage and disk usage for compiled Python files.
To avoid storing the extra information and deactivate printing the extra traceback information, use the -X no_debug_ranges command line option or the PYTHONNODEBUGRANGES environment variable. PEP 654: Exception Groups and except* ¶ PEP 654 introduces language features that enable a program to raise and handle multiple unrelated exceptions simultaneously. The builtin types ExceptionGroup and BaseExceptionGroup make it possible to group exceptions and raise them together, and the new except* syntax generalizes except to match subgroups of exception groups.
See PEP 654 for more details. (Contributed by Irit Katriel in bpo-45292 . PEP written by Irit Katriel, Yury Selivanov and Guido van Rossum.) PEP 678: Exceptions can be enriched with notes ¶ The add_note() method is added to BaseException . It can be used to enrich exceptions with context information that is not available at the time when the exception is raised. The added notes appear in the default traceback. See PEP 678 for more details. (Contributed by Irit Katriel in bpo-45607 .
PEP written by Zac Hatfield-Dodds.) Windows py.exe launcher improvements ¶ The copy of the Python install manager included with Python 3.11 has been significantly updated. It now supports company/tag syntax as defined in PEP 514 using the -V: <company> / <tag> argument instead of the limited - <major> . <minor> . This allows launching distributions other than PythonCore , the one hosted on python.org . When using -V: selectors, either company or tag can be omitted, but all installs will be searched.
For example, -V:OtherPython/ will select the "best" tag registered for OtherPython , while -V:3.11 or -V:/3.11 will select the "best" distribution with tag 3.11 . When using the legacy - <major> , - <major> . <minor> , - <major> - <bitness> or - <major> . <minor> - <bitness> arguments, all existing behaviour should be preserved from past versions, and only releases from PythonCore will be selected. However, the -64 suffix now implies "not 32-bit" (not necessarily x86-64), as there are multiple supported 64-bit platforms.
32-bit runtimes are detected by checking the runtime's tag for a -32 suffix. All releases of Python since 3.5 have included this in their 32-bit builds. New Features Related to Type Hints ¶ This section covers major changes affecting PEP 484 type hints and the typing module. PEP 646: Variadic generics ¶ PEP 484 previously introduced TypeVar , enabling creation of generics parameterised with a single type. PEP 646 adds TypeVarTuple , enabling parameterisation with an arbitrary number of types.
In other words, a TypeVarTuple is a variadic type variable, enabling variadic generics. This enables a wide variety of use cases. In particular, it allows the type of array-like structures in numerical computing libraries such as NumPy and TensorFlow to be parameterised with the array shape . Static type checkers will now be able to catch shape-related bugs in code that uses these libraries. See PEP 646 for more details. (Contributed by Matthew Rahtz in bpo-43224 , with contributions by Serhiy Storchaka and Jelle Zijlstra.
PEP written by Mark Mendoza, Matthew Rahtz, Pradeep Kumar Srinivasan, and Vincent Siles.) PEP 655: Marking individual TypedDict items as required or not-required ¶ Required and NotRequired provide a straightforward way to mark whether individual items in a TypedDict must be present. Previously, this was only possible using inheritance. All fields are still required by default, unless the total parameter is set to False , in which case all fields are still not-required by default.
For example, the following specifies a TypedDict with one required and one not-required key: class Movie ( TypedDict ): title : str year : NotRequired [ int ] m1 : Movie = { "title" : "Black Panther" , "year" : 2018 } # OK m2 : Movie = { "title" : "Star Wars" } # OK (year is not required) m3 : Movie = { "year" : 2022 } # ERROR (missing required field title) The following definition is equivalent: class Movie ( TypedDict , total = False ): title : Required [ str ] year : int See PEP 655 for more details. (Contributed by David Foster and Jelle Zijlstra in bpo-47087 .
PEP written by David Foster.) PEP 673: Self type ¶ The new Self annotation provides a simple and intuitive way to annotate methods that return an instance of their class. This behaves the same as the T
The rest of this page is on the live site.