Blog

Home / Blog

Python‌ ‌3.10‌ ‌New‌ ‌Features‌ ‌

Billy Yann
Data Scientist
Deep learning and machine learning specialist, well-versed with experience in Cloud infrastructure, Block-chain technologies, and Big Data solutions.
October 04, 2021


Python

Python, a general-purpose programming language, is similar to that of JavaScript, C++, C#, and Java. It is simple, flexible, and easy to understand and learn. Frequently used by Data Science Community, it is suitable for a higher level of abstraction, as required by the DevOps Programmers. It can run in any type of Operating System, mainly in the big three that include Windows, Linux, MacOs. The availability of a wide range of libraries makes it attractive. Also, it is free to use and learn.

The Versions

Python language is available in many versions: Python 2, Python 3, and the latest one, which is Python 3.10. When Python 3 got launched, it came with an update, which showcases that any code written for one version will probably not run in the next or updated version and vice-versa. Tools such as the 2-3 Utility will partially automate translation from Python 2 to Python 3. Python 2 got launched in 2000 and Python 3 in 2008. The Python 2 end-of-the-life plan got announced in 2015 and got postponed to 2020. Python 3 and its newer version Python 3.10 gets referred to as the future of Python Language.

Python‌

Python Programming

Several different programming paradigms are involved in the Python Language that allows developers to use the code in the following areas: Procedural Programming, Declarative Programming, Object-Oriented Programming, and Functional Programming. Some programming languages get considered hybrid languages. These allow the developers to utilize a combination of different approaches within the same program. Python is one such hybrid programming language. It allows the developers to write procedural codes and to use objects in an object-oriented manner for writing various functional programs.

Procedural Programming

Program represented as a sequence of instructions. Herein, procedures or functions are used to provide structure to the program.

Declarative Programming

The language/environment determines how a problem gets solved. SQL (Structured Query Language) is one such declarative language.

Object-Oriented Language

Objects form a system wherein each one holds its own data and defines its behavior. Examples include Java and C#.

Functional Programming

Herein, the languages decompose a problem into a set of functions. Each function is independent of any external state operating only on the inputs they received for generating outputs. Haskell, the programming language, is a worthy example of this category.

Python Libraries

The python libraries extend the language-functionality, making it easier for developing various applications. These libraries cover web frameworks, email clients, content management operations, lightweight concurrency, graphics libraries, and machine learning using libraries.

Python Execution Model

Python, the interpreted language, does not require a compilation phase for converting the human-readable format into something, which gets executed by a computer. Herein, the plain text version gets fed into another program that executes the program. Python uses an intermediate model in which the plain text English-style Python program gets converted into an intermediate “pseudo” machine code format. It is this intermediate form that gets executed.

Running Python Programs

The different ways by which a Python program gets run include interactively using the Python Interpreter, storing in a file and running using the Python Command, running as a Script File specifying the Python Interpreter to use within the script file, and from within a Python IDE (Integrated Development Environment) such as PyCharm. Now that we have a basic idea about Python, its versions, Python Programing, Python Libraries, and the ways to run Python Programs along with the Python Execution Model, let us discuss the new features of the latest update of Python, which is the Python 3.10. The Python 3.10 got released on 04 October 2021 with certain brand new features. So, read on and find out more about Python 3.10., and its new features.

Python 3.10.

As of 04 October 2021, Python 3.10 is officially available to all software developers and everyone who wants to use it. The volunteers of Python had been working diligently on this new version since May 2020. Each new version of Python comes out with a host of changes, which include new syntaxes, updated standard library, interpreter movements, typing features, important depreciations, removals or restrictions, and certain new cool features. Try out these new features by running Python 3.10. The language is available from Python Homepage. For this purpose, one can also use Docker with the latest Python Image.

Python 3.10 New Features

Context Managers, Parenthesized

Enclosed parentheses are used in this version for continuation across multiple lines in context managers. This feature allows formatting a long collection of context managers in multiple lines in a similar way it was previously possible with import statements. The previous version, Python 3.9, was not able to use the parentheses, which got rectified when Python 3.10 rolled out.

Best Error Messages

Python 3.10 handles all errors diligently by nullifying them. In the case of Syntax Errors, when parsing code that contains unclosed parentheses gets interpreted, it involves the location of unclosed parenthesis. This helps in rectifying the issue, without displaying the Syntax Error Message. Similarly, the errors involving unclosed string literals now point to the start of the string. Many indentation-error exceptions now have more context regarding what kind of block was expecting an indentation including the correct location. Attribute Errors get resolved through suggestions about similar attribute names in the object line. In the case of Name-Errors, it will offer suggestions of similar variable names in the function.

Precise line numbers for debugging and other tools

This new feature brings in more precise line numbers for debugging, profiling, and coverage tools. Tracing events with the correct line number gets generated for all lines of code executed and only for lines of code that get executed.

For this debugging process, this version of Python (Python 3.10) relies on co_lines(), which returns an iterator over Bytecode offsets and source code lines.

Structural Pattern Matching

In Python 3.10, the Structural Pattern Matching is added in the form of a match statement and case statements of patterns with associated actions. These patterns consist of sequences, mappings, primitive data types, and class instances. Pattern matching enables programs to extract information from complex data types branching on the structural data and applying specific actions based on different data forms.

Usually, a match statement takes an expression and compares its value to successive patterns, which are given as one or more case blocks. It works well with the Declarative Programming Approach.

While an imperative series of instructions using nested if statements could be used for accomplishing something similar to structural pattern matching, it becomes less clear than the declarative approach. The declarative approach states the conditions to meet for a match and is more readable through its explicit patterns. Herein, a simple pattern gets matched to a “literal”. The patterns happen with a “literal” and a “variable”. Classes can be used to structure the data and this pattern can capture class attributes into variables. Patterns get matched with positional parameters and they also can be nested.

Structural pattern matching gets used for detecting and deconstructing different structures of the data. The new feature uses different kinds of patterns and helps in matching literal patterns.

Deconstructing Data Structures: At its core, structural pattern matching is all about defining data patterns to which all your data-structures get matched. One important feature is that at most one pattern gets matched.

Matching Literal Patterns: This type of pattern matches a literal object like an explicit string or number. One limitation is that the developers or users can’t directly match values stored in variables. However, a value pattern gets used for matching the stored values.

A value pattern looks a bit like a capture pattern but uses a previously defined dotted name that holds the value, which needs to be matched against.

New Type Union Operator

This new feature of Python 3.10 simplifies the code. With the current Python version (Python 3.9), if anyone wants to use the “type hints” for a function that has parameters and can receive values or different types, they have to use the Union type. The latest Python 3.10 introduced a new Union Operand - |. This operand highlights that certain parameters can be either type 1 or type 2.

Conclusion

Reliably, each Python release brings some improvements to the static typing system. The improvements brought about by Python 3.10 include: Allow writing union types as X | Y, Explicit Type Aliases, User-Defined Type Guards, and Parameter Specification Variables. Union types can be used for declaring a variable that has one of several different types. Type Aliases allow to quickly define new aliases that stand in for more complicated type declarations. Type Guards help in narrowing down all union types. The type checkers use a few different constructs for narrowing down union types. The final new typing feature is Parameter Specification Variables.

Herein, we discussed Python, its versions, and the new features that came out with the latest version of Python, Python 3.10. Have a good read and get a good idea about these new features.