top of page

Top 5 best new features in Python 3.10



TL;DR



 

Get Started with the best new features in Python 3.10


Python 3.10 is the newest version will release on October 4th 2021, which promises to fix some big bad bugs that is painpoints of existing versions of Python.


For several years, Python become one of the most popular programming language in the world. So do Python's new version 3.10, it wil bring some exciting changes to the language that aims to easy-to-use functionalities for both novice and experienced programmers.


The glaring improvements in the new Python version include:

  • Introduction of structural pattern matching.

  • More efficient error messages

  • New Union Operators.

  • More accurate line numbers for debugging.

Error Messages Improvement

While writing code, almost developers likely understand how importance of error messages for their debug. In the previous verisons of Python, there are many error message types (wrong syntax, missing keywords, incorrect or misspelled keyword, etc.) threw as soon as there caught the problem.


These error messages were too hard for newbie to get started with fixing it, or even advanced users also became tough. For example:


SyntaxError: unexpected EOF while parsing error message.

The lack of clarity in error messages now has been replaced with easier reading and understanding like this:


SyntaxError: { was never closed unexpected EOF while parsing

Or new import error messages will be like:


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

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.


Let see the sample:


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

The code above works, but it's might too long and hard to understand for the first seen. Now with Python 3.10, you can use backslash (\) to break the line and make your code look more elegant.


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

or by parentheses


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


More Accurate 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

Structural Pattern Matching

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.


The match-case statement has been a part of the Python for a long time ago. This statement is created to reduce if-else statement called 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-is
    return media_object
  case 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 automatically recognizes objects like JPG, GIF, and video formats.


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.


Let's check this sample:


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





90 views0 comments
Stationary photo

Be the first to know

Subscribe to our newsletter to receive news and updates.

Thanks for submitting!

Follow us
bottom of page