![]() |
Python releases a new version almost every year. The latest version, that is, Python 3.12 was released on 2 October, 2023. This version introduced many new features and improvements. In this article, we will see some of the newly added features to Python 3.12. Table of Content Download and Install PythonTo download and install Python, follow these steps based on your operating system:
Improved Error Messages in PythonPython 3.12 brings several improvements to error messages, making them more precise and informative. Standard Library Suggestions: Modules from the standard library are now potentially suggested as part of error messages when a NameError is raised to the top level.
Attribute Suggestions for Instances: When a NameError is raised in a method and the instance has an attribute that matches the name in the exception, the suggestion will include self.<NAME> instead of the closest match in the method scope.
SyntaxError Improvement: Syntax error messages are improved when the user types import x from y instead of from y import x.
ImportError Suggestions: ImportError exceptions raised from failed from <module> import <name> statements now include suggestions for the value of <name> based on the available names in <module>.
More Flexibility in Python F-StringPython 3.12 lifts some restrictions on f-strings, allowing for more flexibility in their usage. Quote Reuse: F-strings now support reusing the same quotes as the enclosing f-string, enabling nested f-strings.
Multi-line Expressions and Comments: F-strings can now span multiple lines and include inline comments.
Backslashes and Unicode Characters: F-string expressions can now contain backslashes and unicode escape sequences.
Type Parameter SyntaxIn the Python version below 3.12, type aliases, generic classes, and generic functions were declared using the TypeVar module. But Python 3.12 provides a cleaner and more concise code by declaring generic classes and functions without using the TypeVar module. It can be done by simply using the square brackets.
Additionally, the type statement can be used to declare the type aliases as well as generic type aliases. type Point = tuple[float, float]
type Point[T] = tuple[T, T] # generic type aliases Type HintsThere are two new features added to Python version 3.12 related to Type Hints. They are as follows:
Improvement in ModulesPython 3.12, along with the addition of completely new features also introduced new functionalities and improved the performance of the existing modules. Let us see each module one by one. Array in PythonThe new array module has a feature of subscription, which is included in array.array class. It provides more flexibility and an easier access to the element while working with Python arrays and making it a generic type.
Output: 1 AsyncioPython 3.12 brings a number of improvements to asyncio module. The framework avoids unnecessary copying when writing to sockets improves the performance by utilizing sendmsg() if the platform supports it. The addition of asyncio.eager_task_factory() and asyncio.create_eager_task_factory() functions for opting into eager task execution, also results in faster performance. The child process monitoring is also improved by default usage of asyncio.PidfdChildWatcher on Linux if os.pidfd_open() is available. CalendarAddition of enums calendar.Month and calendar.Day in calendar module, providing standardized representations for months of the year and days of the week. These enums enhance code readability and maintainability when working with calendar-related functionalities. CSVIn CSV module, addition of csv.QUOTE_NOTNULL and csv.QUOTE_STRINGS flags for finer control over handling None and empty strings by csv.writer objects.
ItertoolsA new function in itertools module, itertools.batched() is added for collecting into even-sized tuples where the last batch may be shorter than the rest. This is useful for batch processing of data.
Output: [(1, 2, 3), (4, 5, 6)] MathAddition of math.sumprod() in the math module for computing a sum of products and extension of math.nextafter() to include a steps argument for moving up or down multiple steps at a time. Python OSIn os module, the improvement in the accuracy and performance of os.stat() and os.lstat() on Windows, providing more reliable file system information. Introduction of os.PIDFD_NONBLOCK to open a file descriptor for a process in non-blocking mode. Introduction of new functions like os.listdrives(), os.listvolumes(), and os.listmounts() on Windows for enumerating drives, volumes, and mount points.
PathlibThe pathlib.Path.walk() is added for walking directory trees, similar to os.walk(). The pathlib.Path.glob(), pathlib.Path.rglob(), and pathlib.PurePath.match() now accept a case_sensitive parameter for precise matching control.
RandomThe random.binomialvariate(n, p) is added to the Python random module, which returns the number of successes for n independent trials with the probability of success in each trial being p. ShutilPython 3.12 version has improved a lot of shutil module’s functions. The shutil.make_archive() now passes the root_dir argument to custom archives to improve the flexibility. The shutil.rmtree() accepts a new argument onexc for error handling, replacing the deprecated onerror. While shutil.which() on Windows now consults the PATHEXT environment variable for executable matches.
Sqlite3There is an addition command-line interface for SQLite. Introduction sqlite3.Connection.autocommit attribute and autocommit parameter in sqlite3.connect() for transaction handling control.
StatisticsThe statistics.correlation() has been extended to include a ranked method for computing the Spearman correlation of ranked data.
Output: Output: -1.0 TempfileThe tempfile.NamedTemporaryFile() function now supports a new optional parameter delete_on_close.
ThreadingTwo new functions – threading.settrace_all_threads() and threading.setprofile_all_threads() are added to set tracing and profiling functions in all running threads.
TkinterThe tkinter.Canvas.coords() now accepts coordinates grouped in pairs for increased readability and ease of use.
TokenizeChanges from PEP 701 are included, providing more flexible f-string expression components.
TypesThe types.get_original_bases() is added for further introspection of user-defined generic types when subclassed.
Output: (<class 'list'>,) TypingThe isinstance() checks against runtime-checkable protocols now utilize inspect.getattr_static() for attribute lookup, preventing unexpected evaluation of descriptors and __getattr__() methods during protocol checks. This ensures more accurate and efficient type checking against protocols. Members of runtime-checkable protocols are now considered “frozen” at runtime, improving performance by speeding up isinstance() checks against protocols with a few members. UnicodedataUpdated Unicode database to version 15.0.0, providing the latest Unicode character information.
Output: LATIN CAPITAL LETTER A UnitestA –durations command line option is added to display the N slowest test cases. python3 -m unittest --durations=3 tests.test_module UuidAdded a command-line interface for generating UUIDs. python3 -m uuid Syntactic Formalization of f-stringsThe new Python version has made the f-string more powerful by elimination a lot of ristriction on it. The formatted string, commonly known as f-string was originally introduced in Python version 3.6 for string formatting. The new version allows the f-string to include various valid Python expressions such as, multiline expressions, comments, backslashes, unicode characters, andquote reuse. Per-Interpreter GILPython is an interpreter language and has a Global Interpreter Lock (GIL) which prevents multiple threads to execute simultaneously. In Python 3.12 version, the the sub-interpreters have their own GLI which allows them to utilize the multiple CPU cores efficiently and thus, improve the performance. Low Impact Monitoring for CPythonPreviously, sys.settrace was used by the Python debuggers for debugging and profiling purposes. But this method was less efficient, so in the new Python version, a new set of API for profilers, debuggers for monitoring the CPython events is introduced. This provides a better debugging and profiling capabilities while minimizing the performance impact. ConclusionThese enhancements across various modules contribute to a more robust, efficient, and feature-rich Python ecosystem, catering to diverse development requirements and scenarios. Whether it’s improving debugging capabilities, enhancing filesystem operations, or refining database interactions, Python 3.12 offers a plethora of upgrades to empower developers in building robust and scalable applications. |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |