What’s new in python 3.11: favourite features
Aishvarya Tandon¶
- PhD student - CzodrowskiLab / Waldmann group
- Contact information: Email 1, Email 2, GitHub
Python 3.11 came out little more than a month ago. While it has loads of new features, here are 3 of my favorites.
The following work is based on my opinion. Credits of all the software used and demonstrated belongs to their respective authors and the community.
License: MIT
import sys
print(sys.version)
Speed¶
Official release notes mention:
Python 3.11 is between 10-60% faster than Python 3.10. On average, we measured a 1.25x speedup on the standard benchmark suite.
Few results at:
- Official benchmarks: GitHub Page
- Speed test by Dennis Bakhuis: His GitHub Page
Why is this so important? *These speedup comes without changing your code! Just continue to write Python code properly :)*
Better error messages¶
In Python 3.11, the interpreter now point to the exact expression that causes an error when printing tracebacks, instead of just pointing the lines which was done by the previous versions.
Compared to Python 3.10 or before:
At this moment, it is only visible while using Terminal/console and not inside Jupyter notebook.
Support for parsing TOML in the standard library¶
tomllib is a module which provides an interface for parsing TOML. With this module now present in the standard library, dependence on external modules for parsing TOML is now redundant.
At this moment, this module does not support writing TOML.
import tomllib
with open("data/load_file.toml", "rb") as f:
data = tomllib.load(f)
#The TOML file is now parsed as a dictionary
data
#or simply parse a TOML string
load_string = """
Model_name = "Best_model_ever"
Numbers_of_fold = 5
"""
data2 = tomllib.loads(load_string)
#The TOML formatted string is now parsed as a dictionary
data2
You can read more about TOML in my previous 5 mins of fame notebook.