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.
Download and Install Python
To download and install Python, follow these steps based on your operating system:
- Go to the official Python website: python.org.
- Navigate to the "Downloads" section and click on the "Download Python" button, which will download the latest version suitable for Windows.
- Open the downloaded .exe file to start the installer.
- Check the box that says "Add Python to PATH" at the bottom of the installation window.
Read More: Download and Install Python 3 Latest Version
Improved Error Messages in Python
Python 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.
Python
>>> sys.version_info
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined. Did you forget to import 'sys'?
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.
Python
class A:
def __init__(self):
self.blech = 1
def foo(self):
somethin = blech
A().foo()
Traceback(most recent call last):
File "<stdin>", line 1
somethin = blech
^ ^ ^ ^ ^
NameError: name 'blech' is not defined. Did you mean: 'self.blech'?
SyntaxError Improvement: Syntax error messages are improved when the user types import x from y instead of from y import x.
Python
>>> import a.y.z from b.y.z
Traceback (most recent call last):
File "<stdin>", line 1
import a.y.z from b.y.z
^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Did you mean to use 'from ... import ...' instead?
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>.
Python
>>> from collections import chainmap
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'chainmap' from 'collections'. Did you mean: 'ChainMap'?
More Flexibility in Python F-String
Python 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.
Python
songs = ['Take me back to Eden', 'Alkaline', 'Ascensionism']
f"This is the playlist: {', '.join(songs)}"
Multi-line Expressions and Comments: F-strings can now span multiple lines and include inline comments.
Python
f"This is the playlist: {', '.join([
'Take me back to Eden', # My, my, those eyes like fire
'Alkaline', # Not acid nor alkaline
'Ascensionism' # Take to the broken skies at last
])}"
Backslashes and Unicode Characters: F-string expressions can now contain backslashes and unicode escape sequences.
Python
print(f"This is the playlist: {'\n'.join(songs)}")
print(f"This is the playlist: {'\N{BLACK HEART SUIT}'.join(songs)}")
Type Parameter Syntax
In 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.
Python
def max[T](args: Iterable[T]) -> T:
...
class list[T]:
def __getitem__(self, index: int, /) -> T:
...
def append(self, element: T) -> None:
...
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 Hints
There are two new features added to Python version 3.12 related to Type Hints. They are as follows:
- Override Decorator for Static Typing: The new typing.override() decorator allows the python checkers to check the inherited classes and methods. It intended to override a method in a superclass, helping catch mistakes in type annotations. In simple words, it makes sure that any changes made to the parent class are also reflected in the child class.
- TypedDict for kwargs: Python version 3.12 introduced a better way of writing kwargs in the function signatures. The TypedDict allows to write kwargs with different types, allowing for better checking and validation.
Improvement in Modules
Python 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.
The 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.
Python
import array
arr = array.array('i', [1, 2, 3, 4, 5])
print(arr[0])
Output:
1
Asyncio
Python 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.
Addition 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.
In 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.
Python
import csv
data = [['Alice', None], ['Bob', ''], ['Charlie', 'Developer']]
with open('data.csv', 'w', newline='') as file:
writer = csv.writer(file, quoting=csv.QUOTE_NOTNULL)
writer.writerows(data)
A 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.
Python
import itertools
data = [1, 2, 3, 4, 5, 6]
batched_data = list(itertools.batched(data, 3))
print(batched_data)
Output:
[(1, 2, 3), (4, 5, 6)]
Math
Addition 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.
In 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.
Python
import os
print(os.listdrives())
Pathlib
The 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.
Python
from pathlib import Path
for file in Path('/path/to/dir').rglob('*.txt', case_sensitive=True):
print(file)
Random
The 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.
Shutil
Python 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.
Python
import shutil
shutil.make_archive('archive', 'zip', root_dir='/path/to/dir')
Sqlite3
There is an addition command-line interface for SQLite. Introduction sqlite3.Connection.autocommit attribute and autocommit parameter in sqlite3.connect() for transaction handling control.
Python
import sqlite3
conn = sqlite3.connect('example.db', autocommit=True)
Statistics
The statistics.correlation() has been extended to include a ranked method for computing the Spearman correlation of ranked data.
Python
import statistics
data1 = [1, 2, 3, 4, 5]
data2 = [5, 4, 3, 2, 1]
correlation = statistics.correlation(data1, data2)
print(correlation)
Output:
Output: -1.0
Tempfile
The tempfile.NamedTemporaryFile() function now supports a new optional parameter delete_on_close.
Python
import tempfile
with tempfile.NamedTemporaryFile(delete_on_close=True) as temp_file:
temp_file.write(b"Temporary data")
Threading
Two new functions - threading.settrace_all_threads() and threading.setprofile_all_threads() are added to set tracing and profiling functions in all running threads.
Python
import threading
def trace(frame, event, arg):
print(f"Trace event: {event} in thread {threading.get_ident()}")
threading.settrace_all_threads(trace)
The tkinter.Canvas.coords() now accepts coordinates grouped in pairs for increased readability and ease of use.
Python
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root)
canvas.create_rectangle((10, 10), (50, 50))
canvas.pack()
root.mainloop()
Tokenize
Changes from PEP 701 are included, providing more flexible f-string expression components.
Python
import tokenize
source = 'f"This is {some_var}."'
tokens = tokenize.tokenize(iter(source.splitlines()))
for token in tokens:
print(token)
Types
The types.get_original_bases() is added for further introspection of user-defined generic types when subclassed.
Python
import types
class MyList(list):
pass
print(types.get_original_bases(MyList))
Output:
(<class 'list'>,)
Typing
The 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.
Unicodedata
Updated Unicode database to version 15.0.0, providing the latest Unicode character information.
Python
import unicodedata
print(unicodedata.name('A'))
Output:
LATIN CAPITAL LETTER A
A --durations command line option is added to display the N slowest test cases.
python3 -m unittest --durations=3 tests.test_module
Added a command-line interface for generating UUIDs.
python3 -m uuid
The 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 GIL
Python 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 CPython
Previously, 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.
Conclusion
These 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.
Similar Reads
What's new in Python 3.9?
A new version of Python is coming! Soon, we'll be using it in our Python projects. As of 20/07/2020 Python 3.9 is in beta version(3.9.0b4) and will release the full version of Python 3.9 pretty soon. Enough of introduction. Let's get started and learn about the new features. 1. Dictionary merge and
3 min read
How to Download Python Old Version and Install
Python is a popular programming language known for its simplicity and versatility. When we install Python in our systems we always install the latest version of it but there may be situations where we need to install an older version of Python due to compatibility reasons or to work on legacy projec
2 min read
Download and Install Python 3 Latest Version
If you have started learning of Python programming, then for practice, Python installation is mandatory, and if you are looking for a guide that teaches you the whole process from downloading Python to installing it, then you are on the right path.Here in this download and install Python guide, we h
6 min read
How to download and install Python Latest Version on Windows
Python is a widely-used general-purpose, high-level programming language. This article will serve as a complete tutorial on How to download and install Python latest version on Windows Operating System. Since windows don't come with Python preinstalled, it needs to be installed explicitly. In window
2 min read
How to download and install Python Latest Version on Android
Python is a widely-used general-purpose, high-level programming language. This article will serve as a complete tutorial on How to download and install Python latest version on Android. Python can run on Android through various apps from the play store library. This tutorial will explain how to run
2 min read
How to downgrade python version in Colab?
Google Colab is an incredibly versatile platform for data science and machine learning. It offers a virtual environment equipped with a variety of pre-installed software tools, and the best part is, that it's free! In this guide, we'll take you through the process of downgrading the Python version i
9 min read
How to Download and Install Python Latest Version on Linux?
Python is a widely-used general-purpose, high-level programming language. This article will serve as a complete tutorial on How to download and install Python latest version on Linux Operating Systems. On every Linux system including following OS, Ubuntu Linux Mint Debian openSUSE CentOS Fedora and
3 min read
How to Install PyTorch on Python 3.12.1
Currently, PyTorch does not support Python 3.12, and users attempting to install it on this version will encounter compatibility issues. This article will guide you through the current state of PyTorch installation on Python 3.12, alternatives, and potential workarounds.Table of ContentCurrent Statu
4 min read
How to Learn Python in 21 Days
At present, Python is one of the most versatile and demanded programming languages in the IT world. Statistically, there are around 8-9 Million Python developers across the world and the number is increasing rapidly. Meanwhile, the average salary of an Entry-Level Python Developer in India is around
9 min read
Version Control for Python Projects: Why and How to Use Git
In todayâs fast-paced software development environment, managing changes to code efficiently is crucial. Version control systems (VCS) are essential tools for developers, enabling collaboration, tracking changes, and maintaining a history of project modifications. This article delves into the import
5 min read