What’s new in Python 3.10 ?

Python.org has released Python3.10.0 on 8 june 2021. Everyone wants to know what new features are added in this update?

I will walk you through these new features.

Parenthesized context managers

Context managers are special code constructs that allow a simple handling of resources. Now we can use multiple context managers in one. 

By enclosing parentheses for continuation across multiple lines in context managers we can achieve multiple context managers. 

Better error messages

Python 3.10 provides us with more helpful error messages. This has support for SyntaxError,IdentationError,AttributeError, and NameError.

So in place of 

SyntaxError: invalid syntax 

it displays

SyntaxError: ‘{‘ was never closed

This will definitely help in debugging and writing codes easily.

Structural Pattern Matching

Python 3.10 has introduced switch statements. 

Syntax:

  match subject:

    case <pattern_1>:

        <action_1>

    case <pattern_2>:

        <action_2>

    case <pattern_3>:

        <action_3>

    case _:

        <default_action>

A match statement takes an expression and compares its value to successive patterns given as one or more case blocks. 

New Type Union Operator

A new type union operator was introduced which enables the syntax X | Y. 

Example:

isinstance(1, int | str)

Happy learning!

Subscribe to newsletter and receive latest tech updates .

Advertisements

One response to “What’s new in Python 3.10 ?”

Leave a Reply

Discover more from Chandan Rajpurohit

Subscribe now to keep reading and get access to the full archive.

Continue reading