• Chandrakumar Murugesan

Python 3.11: Release Note


After a month of trial runs with new and existing projects, I like to share a few highlight features of Python version 3.11.


"Faster Python"

The first word that comes to my focus is "Faster Python", as claimed, I wanted to test it first. I selected Count to Billion as the first test script to determine if this Python is faster. For comparison, I pick the previous version, 3.10.


3.11 took less time compared to the time taken by 3.10 to complete the Count to Billion test. 3.11 completed the testing in 1m 34, whereas 3.10 took 2m 29 sec. It means 3.11 took 64% of the time taken by 3.10 to complete.


I was so excited, so I decided to go for more testing. Now, I picked Python's pyperformance as the right tool for this. In addition, Python 3.8 is also included in the comparison list making it more exciting. So now the fight between 3.8 vs 3.10 vs 3.11. The following image illustrates the result.

3.8 vs 3.11
3.10 vs 3.11

So the verdict is unambiguous that Python 3.11 is faster than the previous version.


Exception in detail

In this version, one of the essential changes is displaying the exception in detail. I am sure this feature is for lazy people like me. The following example shows the exception output with a pointer to point out what causes the exception. Wow, 1000 kisses to the team who build this.

from typing import Self

class StringBuilder:
    
    def __init__(self, data:str = ""):
        self.data = data
     
    def append(self, data:str) -> Self:
        self.data += data
        return self
        
    def __str__(self) -> str:
        return self.data

class Cmd:
    
    def __init__(self):
        self.ls = "ls"

if __name__ == "__main__":
    sb = StringBuilder("$ ").append(Cmd.clear)
 
    print(sb)
    
Output
 Traceback (most recent call last):
 File "/home/cmurugesan/forblogs/test_python3_11/self_test.py" , line 21, in <module>
sb = StringBuilder("$ ").append(Cmd.clear)
                                ^^^^^^^^^
AttributeError: type object 'Cmd' has no attribute 'clear'

Typing: Self, Required, and NotRequired

The 3.11 version loaded with many new additions in the typing module. But I will list a few of them that I love most.


"Self". I expected this for a long time. I was always unhappy to set the return type of the class's function, which returns the self as the class name. Now it is apparent and makes sense. The following code comparison shows how clear its.


On 3.10


class StringBuilder:

    def __init__(self, data:str = ""):
        self.data = data

    def append(self, data:str) -> StringBuilder:
        self.data += data
        return self

    def __str__(self) -> str:
        return self.data


class Cmd:

    def __init__(self):
        self.ls = "ls"

if __name__ == "__main__":
    sb = StringBuilder("$ ").append(Cmd.ls)

    print(sb)
    

On 3.11


from typing import Self


class StringBuilder:

    def __init__(self, data:str = ""):
        self.data = data

    def append(self, data:str) -> Self:
        self.data += data
        return self

    def __str__(self) -> str:
        return self.data


class Cmd:

    def __init__(self):
        self.ls = "ls"

if __name__ == "__main__":
    sb = StringBuilder("$ ").append(Cmd.ls)

    print(sb)

Required and NotRequired is other expected change in the typing module. TypedDict now has this extra wing, which will help the developer have more control.


from typing import Required, NotRequired, TypedDict
from enum import Enum

Gender = Enum('Gender', ['FEMALE', 'MALE', 'TRAMSGENDER_MEN', 'TRAMSGENDER_WOMEN'])

class Person(TypedDict):
    
    name: Required[str]
    age: Required[int]
    gender: NotRequired[Gender]
    
    
if __name__ == "__main__":
    
    print(Person(name="Chandrakumar", age=43)) # Accepted
    print(Person(name="Chandrakumar", gender=Gender.MALE)) # Rejected - Age is required





17 views0 comments

Recent Posts

See All