Cool new features of Python 3.10
An introduction to some neats new features of Python 3.10.
Despite all the focus on the metaverse or NFTS, you might have recently heard that Python has reached 1st place in the TIOBE Index(more details here).
This news could not have been a better time, as, on 4th October 2021, version 3.10 of Python was released to the masses!!
This article will introduce you to three significant changes brought to the community by the latest version of my favorite programming language !!
1. Structural Pattern Matching
The first and most exciting change involves introducing a new syntax for structural pattern matching via PEP 634, 635, and 636 (If you don't know what a PEP is, check out this page).
Similar to some programming languages like C or javascript, the new pattern matching syntax consists of a match statement and case statements of patterns with associated actions.
A match statement takes an expression and compares its value to successive patterns given as one or more case blocks.
Pattern matching is compatible with primitive data types, class, Enum, dictionary, and complex data types.
Such new syntax will allow us to avoid any long `if … elif … elif` idiom and provide us with a more robust alternative.
2. New Type Union Operator
Python is a dynamic typing language (values have types, but variables do not), the possibility to provide type hints in your code has been quite successful thanks to the typing module.
It has allowed developers to understand what is happening within the code and much more (maybe I should make an article expending on this topic ?).
Thus the second interesting new feature of Python 3.10 is introducing the "|" character as a new way to the "Union" type hint.
This change provides us with a cleaner and more concise way to express `either type X or type Y` instead of typing.Union.
Example of utilizing the existing typing.Union
example of using the new type union operator:
This new syntax can also be used as a second argument within the isinstance() and issubclass() method, making it even more enjoyable.
This new feature will provide much-needed clarity and readability in some of your codebase and deliver less wordy docstring.
3. Deprecation of the package 'distutils'
Last but not least, the package 'distutils' is deprecated and has been replaced by the more commonly used packages' setuptools' and 'packaging'.
'distutils' has been the standard tool for packaging in Python and was part of the standard library (Python 2 and Python 3). This package has been helpful for simple Python distributions but lacks features.
Python 3.10 has many more features, such as better error messaging, type aliases, etc.
Please feel free to explore them here and let me know which ones you are most excited about using!!
As always, clap, follow, and constructive criticisms are always welcome !!
Godspeed and happy coding !!