What’s New in The Latest Version Python 3.10
The usage of indentation in Python, a high-level, general-purpose programming language, emphasises code readability. Python is the most popular programming language in the global developer community, according to the TIOBE Index for October 2021.
Python is a programming language that is extremely versatile, adaptable, and interactive. Python is used in a variety of sectors in the technology industry, most notably web development, machine learning, artificial intelligence, and data science.
Python is a simple language that needs less time and resources to learn as compared to other high-level languages such as Java or C++.
Python 3.10 was published on October 4, 2021, just a few days after the programming language's 30th anniversary. Python contributors recently shifted to an annual update schedule in order to provide new features and enhance old ones.
While there are various new features in the newest in-development version, the most important change has to be the language syntax, as async is now supported.
The most anticipated feature of Python 3.10 is structural pattern matching. This addition adds a switch/case-like capability to Python, similar to what we have in other programming languages.
Python 3.10 Structural Pattern Matching adds certain more capabilities to the switch-like functionality, making it even more powerful than the standard switch conditional statement.
Python replaces the switch keyword with the case keyword to define each separate situation. Here's an example:
x = 3 + 4
match x:
???case 9:
???????return "That's too high..."
???case 5:
???????return "You're two steps away ..."
???case 6 | 8:? # Combining many literals with `|`
???????return "Oops! That was so close!"
???case 9:
???????return "Too small..."
???case _:
???????return "That's too random!"
The preceding example shows how to use Python 3.10's match/case conditional statement. Python, like "case 6," allows you to compare multiple values in the same case statement by utilizing the or | operator.
Similar to other programming languages, Python 3.10 enables you to use wildcards (_) like in the final case statement to act as a default value.
The ability to match complicated patterns is one of the added capabilities that makes Python 3.10's match/case conditional statement more powerful than the switch statement in other programming languages.
Using the match statement, we utilized Python tuple to match complicated arguments against each other. As we saw in the preceding example, the wildcard (_) may also be used to represent the default value.
Guards are another element of Python 3.10 Structural Pattern Matching. Guards let you to use the top pattern with a Guard, an if statement that analyses the following case statement if the Guard evaluates to true. Here's an example:
match input:
???case (x, y) if y > MAX_INT and x > MAX_INT
???????print("Got a pair of large numbers")
???case x if x > MAX_INT:
???????print("Got a large number")
???case (x, y) if x= y
???????print("Got equal items")
???case _:
???????print("Not an outstanding input")
Classes, literals, and variables, as well as nested patterns, allow you to build sophisticated conditional statements in Python 3.10.
Exact Error Messages:
This is probably one of the most useful update in the latest version. Debugging code in prior versions of Python was challenging owing to inadequate error messages. Previous Python versions' error messages issued by the Parser are generic.
print("Hello Reader"
print("Welcome to The Chief IO")
#The Error Message
File "<string>", line 2
???print("Welcome to The Chief IO")
领英推荐
???^
SyntaxError: invalid syntax
The produced error message indicates that the problem is on line 2, when the real error is in line 1. This makes debugging difficult.
Python 3.10 provides a more accurate and line-specific error message, supporting you in determining the source of the problem. Python 3.10 will provide a clearer error message for the same example as above;
File "<string>", line 1
???print("Hello Reader")
???^
SyntaxError: ‘(’ was never closed.
Population Estimate:
Python 3.10 introduces a new function, bit count. This function returns the number of ones in an integer's binary form.
This technique, also known as Population Count (popcount), allows you to count non-zero bits in an integer in a speedier manner.
Backward compatibilities are being deprecated and removed:
From Python 3.10 onwards, some early support for abstract base classes and old import semantics will be phased out.
Some of the deprecated syntax includes find loader()/find module, load module and load attribute.
Distutils support is likewise being phased out in favour of setuptools and packaging.
Strict Zipping:
In Python 3.10, a new optional Keyword parameter, strict, is added to the zip method. As soon as you set the strict keyword to truth, all the iterables in your zip must have the same length, otherwise, you will get an error.
Lastly here is a list of newly added features:
·???????Syntax enhancements
·???????PEP 634: Specification for Structural Pattern Matching
·???????PEP 635: Motivation and Rationale for Structural Pattern Matching
·???????PEP 636: Tutorial on Structural Pattern Matching
·???????Parenthesized context managers
·???????A new functionality has been added to the standard library.
·???????PEP 626 – Add Zip Checking
·???????Enhancements to the interpreter
·???????PEP 626 specifies accurate line numbers for use in debugging and other tools.
·???????New features for typing
·???????PEP 604 – Allowing union types to be written as X | Y
·???????PEP 613 – Type aliases that are explicit
·???????PEP 612 — Variables for parameter definition
·???????Deprecations, deletions, or restrictions:
·???????OpenSSL 1.1.1 or later is required.
·???????Deprecate the distutils module in PEP 632.
·???????PEP 623 – Deprecate and plan for the elimination of PyUnicode Object's wstr member.
·???????PEP 624 – Disable the Py UNICODE encoder APIs
·???????PEP 597 – Include an optional Encoding Warning.
?
REFERENCES: