Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python 3.12 - What's New and How to Download?
Next article icon

Python 3.12 - What's New and How to Download?

Last Updated : 20 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 Python
  • Improved Error Messages in Python
  • More Flexibility in Python F-String
  • Type Parameter Syntax
  • Improvement in Modules
  • Syntactic Formalization of f-strings
  • Per-Interpreter GIL
  • Low Impact Monitoring for CPython
  • Conclusion

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:

  1. 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.
  2. 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.

Array in Python

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.

Calendar

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.

CSV

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) 

Itertools

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.

Python OS

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) 

Tkinter

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

Unitest

A --durations command line option is added to display the N slowest test cases.

python3 -m unittest --durations=3 tests.test_module

Uuid

Added a command-line interface for generating UUIDs.

python3 -m uuid

Syntactic Formalization of f-strings

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.


Next Article
Python 3.12 - What's New and How to Download?

T

tanyasehr88x
Improve
Article Tags :
  • Python
  • Python Blog
Practice Tags :
  • python

Similar Reads

    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 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences