What New Features Python 3.10 Has To Offer ?

What New Features Python 3.10 Has To Offer ?

Python3.10 comes with some notable and useful updates !


4 min read

๐ŸŽ‰ Recently, Python 3.10 has released beta v2 and beta v3 is planned for 17 June 2021. We may see the final release on 4 Oct 2021.

โš™๏ธ How to Get Your Hands On Latest Build ?

  • I have tried this on ubuntu, so below instructions are only for linux users.
# Download the latest version from https://www.python.org/ftp/python/3.10.0
# For now the latest version is beta 3
wget https://www.python.org/ftp/python/3.10.0/Python-3.10.0b2.tgz
# Unpack the tar python file
tar xzvf Python-3.10.0b2.tgz
# cd to Python-3.10.0b2
cd Python-3.10.0b2
# Compile Python source with static libraries
./configure --prefix=$HOME/python-3.10.0b2
make install
  • For Windows users use the Link, to download the beta version 2.
  • For Mac users, use the Link to download beta version 2.

๐Ÿ“• Check the Link, have a look to all the releases of Python3.10 for all operating systems so far.

โœ”๏ธ Once we are done with setting up the new python release in our system.

๐ŸŽ Let's check what awesome features it has to offer.

Below are Some of the notable features of Python3.10

โšก Simplified representation of union using |

overloading the | operator on types to allow writing Union[X, Y] as X | Y

# FUNC: function to accept a input and return it 

# OLD WAY : We have to use Union to mention the types
#           of input function is expecting
def func(value: Union[int, float]) -> Union[int, float]:
    return value

# NEW WAY : We can use | operator to seprate different
#                        types of input
def func(value: int | float) -> Union int | float:
    return value


This feature can also be used for isinstance() and issubclass() functions

# Cheking if the demo varibale is of type str or int
demo = 3
isinstance(demo, int | str)
# OUTPUT --> True

โšก Improved and specified Error Messages

Before in most cases Python returns a more generalised error, which sometimes take time in finding out the actual problem.

Let's have a look, what we are dealing with

>>> print("Hi, there"
... print("how are you?")
  File "<stdin>", line 2
    print("how are you?")
SyntaxError: invalid syntax

>>> print("Hi, there"
... print("how are you?")
  File "<stdin>", line 1
    print("Hi, there"
SyntaxError: '(' was never closed


Also, a new keyword parameter has been added to ZIP function strict=True , which raises the ValueError if the arguments are exhausted at differing lengths.

# Zipping two lists of differnet lengths

zip([2, 4, 6], [9, 10], strict=True)

ValueError: zip() argument 2 is shorter than argument 1

โšก Structural Pattern Matching

This one is my favourite among all the other updates. We have Switch/case statements in other programming languages but we Python users doesn't have this feature ... yet, its here now.

Python Pattern Matching not only gives you levearge to use case statements but it also allows use match semantics, guards (a guard is an arbitrary expression attached to a pattern and that must evaluate to a "truthy" value for the pattern to succeed) and patterns (it allows almost all patterns like OR, AND, AS, mapping, class etc)

  • Simple Case statements
def Animals(x):
    match x:
        case "cat":
            return "mammal"
        case "snake":
            return "reptile"
        case "bees" | "beetles":  # Multiple literals can be combined with `|`
            return "reptiles"
        case _:
            return "Not fall in any category"
  • Pattern Matching
def pets(x):
    match x:
        case {"type": "cat", "name": name, "pattern": pattern}:
            return Cat(name, pattern)
        case {"type": "dog", "name": name, "breed": breed}:
            return Dog(name, breed)
        case _:
            raise ValueError("Not a suitable pet")
  • Guards Matching
def sort(seq):
    match seq:
        case [] | [_]:
            return seq
        case [x, y] if x <= y:
            return seq
        case [x, y]:
            return [y, x]
        case [x, y, z] if x <= y <= z:
            return seq
        case [x, y, z] if x >= y >= z:
            return [z, y, x]
        case [p, *rest]:
            a = sort([x for x in rest if x <= p])
            b = sort([x for x in rest if p < x])
            return a + [p] + b

โšก Other Updates

There are other great changes included in 3.10 release like

โœจ Concluding

Python3.10 packs some interesting features which we all find interesting using in our codebases but it still in beta and will take time to be fully tested and released.

Hope you have learnt something new today!

๐Ÿ™Œ Please show your support and love, it really encourages me to deliver more.๐Ÿ˜

Did you find this article valuable?

Support Abhishek Saini by becoming a sponsor. Any amount is appreciated!