MakeUseOf logo

How Python 3.12 Will Help Improve Your Debugging

A person holding an orange and blue Python sticker
Img By: Pexels ---- No attribution required
img src: https://www.pexels.com/photo/person-holding-an-orange-and-blue-python-sticker-11035474/
Paul is a gadget enthusiast who believes in technology's power to change the world. He is known for his engaging and informative articles, making him a writer to watch in the tech world.
Sign in to your MakeUseOf account

Python plans to release version 3.12 of the language in October 2023. This version brings an array of thrilling features and improvements. Its goal is to improve your coding experience by offering a more efficient and powerful platform, enabling you to enhance your coding skills.

1. Enhanced Error Messages

Python has continually improved its error messages in recent versions. The 3.11 release introduced more accurate error messages by indicating the exact positions within lines.

Python 3.12 takes these enhancements to the next level, with the following additions:

  • Suggestions for missing modules ("did you forget to import X?") now encompass modules from the standard library.
  • Improved error suggestions for common syntax errors related to imports. For instance, if you have import p from m in your code, an error will prompt asking if you intended to write from m import p.
  • Import errors for a given module now include suggestions from the namespace of the imported module.
  • NameError suggestions now also incorporate self added to the name when raised within a class instance. For example, if the name speed is not defined, the error message will propose self.speed as a potential correction. This is particularly useful since omitting self for instance variables is a common source of errors in class instances.

2. Support for the Linux Perf Profiler

The widely used Linux profiler tool, perf, can now work with Python 3.12 programs. In the past, it only focused on tracking C-level operations within the Python runtime.

With Python 3.12, you can now activate perf to collect details about Python programs as well. You can activate it at either the environment level or within a Python program using the sys.activate_stack_trampoline function.

3. Type Hinting Improvements

Since its introduction in Python 3.5, Python's type-hinting syntax has enabled linting tools to detect a wide array of errors in advance. With each subsequent release, Python's type system has evolved to encompass a broader range of use cases, offering enhanced typing features.

TypedDict

Python 3.12 introduces the use of TypedDicts as a source for hinting keyword arguments in functions. This utilizes the Unpack variadic generic, which version 3.11 introduced.

Here's an example illustrating this feature:

class Movie(TypedDict):
 name: str
 year: int
def foo(**kwargs: Unpack[Movie]) -> None: ...

In this case, the function foo can receive keyword arguments with names and types that align with the contents of the Movie TypedDict: name of type str and year of type int.

This enhancement proves valuable when type-hinting functions that accept optional keyword-only arguments without default values.

Type Parameter Syntax

Python 3.12 introduces a more concise syntax for specifying types in generic classes, functions, or type aliases.

Consider the following:

# the old method
from typing import TypeVar
_T = TypeVar("_T")
def func(a: _T, b: _T) -> _T:
 ...
# the new type parameter method
def func[T](a: T, b: T) -> T:
 ...

With the new syntax, there is no need to import TypeVar explicitly. Instead, you can utilize the func[T] syntax to indicate generic type references. It is also possible to define type bounds, such as specifying that a type must be one of a group of types. However, these types themselves cannot be generic. An example of this syntax is func[T: (str, int)].

4. Garbage Collection

In Python 3.12, the garbage collection (GC) mechanism has changed. Previously, the GC ran whenever an object was allocated.

However, starting from Python 3.12, the GC operates exclusively within the eval breaker mechanism in the Python bytecode loop. In other words, it runs between the execution of one bytecode and the next.

Additionally, the GC also triggers when CPython's signal-handler-checking mechanism is invoked. This adjustment enables periodic garbage collection during long-running calls to C extensions outside the runtime.

Unveiling Python 3.12

Python 3.12 introduces a range of exciting features and improvements, revolutionizing the coding experience for you. With enhanced error messages, improved typing capabilities, support for the Linux Perf Profiler, and optimized garbage collection, Python 3.12 equips you with the tools to write high-quality code efficiently. Embrace the power of Python and elevate your coding skills to new heights.

AltStyle によって変換されたページ (->オリジナル) /