top of page
Writer's pictureThe Tech Platform

Python 3.10: Top 6 Useful Features of Python

Python 3.10 boasts of being one of the most stable versions available for users to download, and will be released later this year. The newly released version will come with many noticeable changes, such as easy-to-use functionalities for both novice and experienced programmers.



The glaring improvements in the new Python version include the introduction of structural pattern matching, better error messages, new union operators, accurate line numbers for debugging, and much more.



1. Structural Pattern Matching in Python 3.10

Structural Pattern Matching makes code writing a cinch, and it continues to be one of the prominent highlights of the latest Python version. Python aims to improve the pre-existing match-case statements present in the previous versions of the programming language. Itā€™s made an update to the existing match-case statements within Python.


Letā€™s take a quick peek into the implementations of Structural Pattern Matching:

The match-case statement has been a part of the Python language for a while now. This statement is basically used to avoid the tedious work of writing the if-else statement multiple times.


You can match against objects with similar properties using this feature in the new build.

match media_object:case Image(type="jpg"):# Return as-isreturn media_objectcase Image(type="png") | Image(type="gif"):return render_as(media_object, "jpg")case Video():raise ValueError("Can't extract frames from video yet")case other_type:raise Exception(f"Media type {media_object} can't be handled yet")

The new python library recognizes objects like jpg, gif, and videos. This code can run seamlessly without throwing an error.


2. Improved Error Messages

Every coder likely understands the importance of errors while writing code, and how infuriating some error types can be. The previous versions of Python threw error messages as soon as there were problems in the syntax. These could be due to the wrong syntax, missing keywords, incorrect or misspelled keywords, amongst other issues.


These error messages were far from perfect as it became tough for beginners (at some times, even advanced users) to identify the real cause of the error in their codes. As a programmer, Google continues to be your ally in deciphering the reason behind different error messages.

For example, many people might not know why Python throws the following error:

 SyntaxError: unexpected EOF while parsing error message.

The lack of clarity in such statements prompted the newest Python version to improve on its existing set of error messages.



The older messages have been replaced with easy to understand error messages like:

{ was never closed unexpected EOF while parsing


Some more changes include:


Attribute errors like:

from collections import namedtoplo

module 'collections' has no attribute 'namedtoplo'. Did you mean: namedtuple?



NameError messages are modified to look like:

new_var = 5print(new_vr)>

NameError: name 'new_vr' is not defined. Did you mean: new_var?




3. Parenthesized Context Managers

The new Parenthesized Context Managers can make your code look more elegant. Even though it's not a major feature, it can easily make your code less clunky. This feature is beneficial if you work in a team and your code needs to be structured.


Imagine writing a statement like:

with open('file1.txt', 'r') as fin, open('file2.txt', 'w') as fout:fout.write(fin.read())

The code above works, but the first line is way too long and looks clumsy. You can break the line using a backslash (\) and make the code look structured:

with open('file1.txt', 'r') as fin, \ open('file2.txt', 'w') as fout: fout.write(fin.read())

With the introduction of the new Parenthesized Context manager, you can also break the line using parentheses too:

with (open('file1.txt', 'r') as fin,open('file2.txt', 'w') as fout):fout.write(fin.read())


4. New Type Union Operator

A small but handy feature in Python 3.10 is the new type of union operator. Every Python release comes with a pre-defined set of type-hint features.


The union operator includes conditional logic; for example, int or float can be written as Union[X, Y]. The new union operator can be expressed like int|float also.


The introduction of a new union operand in Python 3.10 is time-saving and makes the code look well-defined.


For example:

def f(x: int | float) -> float:    return x * 3.142f(1)  # passf(1.5)  # passf('str')  # linter will show annotation error



5. Precise Line Numbers for Debugging

You may have noticed many times before that error tracing does not redirect you to the correct line where an error has occurred. This makes debugging difficult for coders who have just started writing code.

The flawed error tracing is especially evident while writing sys.settrace and related tools in Python. The newer version improves this significantly, and you can see precise line numbers when an error occurs.

To bring a more precise line number, Python 3.10 shifts its reliability from the current co_Inotab attribute and uses the new method co_lines() attribute. This attribute works in a way such that the f_lineo always contains the accurate line number.

1. for (2. x) in [1]:3. pass4. return


6. Postponed Evaluation of Annotations

Within Python, the evaluation of type annotation is performed at function definition time. This means that type annotations are evaluated line-by-line in a top-down fashion.

Even though it might seem like the best option, there are still two problems to this approach:

  • Type hints refer to types that aren't defined yet and do not work; these hints need to be expressed as strings.

  • The module imports slowed down as type hints are executed in real-time.

To avoid execution issues, annotations are stored in _annotations_ and evaluation is performed together. This allows forward referencing as module imports are executed first, thereby reducing initialization time.



Source: Paper.li


The Tech Platform

0 comments

Comments


bottom of page