New Python 3.11: What is the Best Time to Upgrade?

N

The latest version of Python – Python 3.11 – is out now. But should developers opt for an upgrade just after its release? This post will talk about some of the major improvements as well as the current challenging areas of the newest Python version to help you decode the right time to switch to it.  

The Evolution Of Python

The legacy of Python is not unfamiliar to most people. Even if you don’t know how to code, chances are you have already heard of Python. What started just as a hobby project named after a British comedy group soon went on to become one of the most sought-after programming languages across the world. 

It’s been a little over three decades since the first version of Python was released. Its high level of abstraction makes it a more suitable choice for data analytics, AI, and ML projects instead of business and marketing-based products. Hence, compared to other interpreted languages like Java or PHP, Python is relatively slower.  

And the creator of Python, Guido Van Rossum, wanted to change that. As he stated in the last Python Language Summit, he has been planning to increase Python’s speed by 4 times. And this vision resulted in the roll out of the latest version of this programming language on 24th October 2022. 

Introducing Python 3.11, which has come with some exciting feature improvements for developers and tech enthusiasts to tinker with. But should you migrate to it right now? If not, when is the right time to do it?   

In order to make that decision and find out whether it is as good as it’s supposed to be, we have to dig deep into the significant changes the newer Python version has brought to the world, along with some newly found issues.

Significant Changes of Python 3.11

1.25X Faster 

Python 3.11 comes with a set of distinct performance improvements, but the specializing adaptive interpreter is by far the most important one. Since the type of an object doesn’t often change, the interpreter is now able to replace generic bytecodes with type-specific codes by checking the running code.  

For example: 

  • Specialized versions for strings, floats, and integers can take the place of binary operations (add, subtract, etc.). 
  • Function calls also require less overhead. 
  • Stack frames for function calls are now more effectively designed and require less memory. 
  • Recursive calls are more efficient than in earlier versions, even if they aren’t tail-optimized (which is quite impossible in Python, anyhow).  
  • The essential modules required for the Python runtime are also stored and loaded more effectively. 
  • Python interpreter itself launches more quickly. 

Python 3.11 is almost 1.25X faster than the last version, as per the official Python Performance Benchmark Suite. Note that this increase in speed is a total measurement. While many objects run a little faster or about the same speed as before, some things work substantially quicker. The fact that these improved features are free is still the greatest part of the release. Python 3.11’s increased speed can be beneficial for Python scripts without the need for any code modifications. 

Improvements in Exception 

Exception is the error-handling method of Python, which has gone through major changes in the latest version. Take a look at them: 

  • There has been a 10% reduction in the amount of time required to detect an exception. 
  • Unless you actually raise exceptions, they no longer have cost to a program. Therefore, the try/except block’s default path requires less memory and is faster. 
  • With the new ExceptionGroup exception type and except* syntax, you can raise and manage a number of exceptions simultaneously. This enables the graceful handling of problems, allowing developers to raise several errors at once. 
  • Contextual notes can be added to exceptions in addition to the exception’s text. 

Improvements in Error Messages 

The addition of more descriptive tracebacks is one of the key changes in Python 3.11. Improvements were made to Python 3.10 by adding better and more descriptive errors. Python 3.11 goes one step ahead by providing improved descriptions of the precise error position. Have a look at this example for a better understanding: 

Here’s a code with syntax error: 

def average_grade(grades): 
    return sum(grades) / len(grades) 

average_grade([]) 

As a response to this, Python 3.10 shows the following message: 

return sum(grades) / len(grades) 
ZeroDivisionError: division by zero 

And here’s what Python 3.11 shows: 

return sum(grades) / len(grades) 
           ~~~~~~~~~~~~^~~~~~~~~~~~~ 
ZeroDivisionError: division by zero 

As you can see, the improved traceback makes it much simpler to determine the exact location of the error. You could only view the exception itself and the line where it was raised in earlier versions. 

Improvements in Type Hints 

Developers can now analyze and manage large codebases in an easier way with Python 3.11’s improved typing hinting features. The Python team has been improving this feature in the last couple of versions, and the latest one added a zing to it. 

Self Type 

Previously, Python programmers had to use difficult and verbose annotations for class methods that return self. Bur now, typing.Self enables you to annotate a class method’s return value as Self; – yes, as simple as that. Your analysis tools for these methods provide you with practical and predictable results. 

Variadic Generics 

You can have variables that include various types, thanks to variadic generics. This is crucial when representing arrays with multiple dimensions using tensor type objects, such as Pandas, TensorFlow objects, NumPy arrays, etc.). 

The 3.11 update introduced the new TypeVarTuple, which enables variadic generics by allowing parameters to have any number of types, such as a variadic type variable. 

Literal String Type 

Prior to the release of Python 3.11, there was no way for type annotations to show that a specific variable had to be a string literal. It is now fixed by the LiteralString annotation. The new annotation enables literal strings as well as strings made up of other literal strings. If programmers run shell and SQL commands to add an extra measure of security and mitigate injection attacks, this feature can be utilized for type checking. 

New TOML Library 

TOML-format files are now supported by the new tomllib library. It is built on top of the tomllib library and writing TOML is not supported by tomllib

tomllib has brought two primary functions to us: 

loads(): load from str  

loads(): load bytes from file 

Mentioned below is an example of each: 

import tomllib 
 
# gives TypeError, must use binary mode 
with open('t.toml') as f: 
    tomllib.load(f) 
 
# correct 
with open('t.toml', 'rb') as f: 
    tomllib.load(f) 
 
# correct 
with open('t.toml') as f: 
    tomllib.loads(f.read()) 
 
# gives TypeError, can't read bytestring 
with open('t.toml', 'rb') as f: 
    tomllib.loads(f.read()) 

A Few Issues with Python 3.11 

As with any new release, there is a few issues in Python 3.11 that makes it a bit difficult to work with it immediately. However, all of this should not be taken as negative remarks. The Python team is providing incredibly valuable work for free, and it takes a long time to finally release a stable and perfect version of a technology. 

Taking that into consideration, some of the notable problems with the new version of Python are: 

  • Incompatible packages 
  • Missing binary packages 
  • No support for toolchain 
  • Lack of Conda support 

Why and When Should You Upgrade to Python 3.11? 

You might feel like avoiding upgrading Python versions as long as you can since it requires work – some changes to your code followed by testing. Why stress over compatibility issues and new releases when you can just use your present version indefinitely? 

The problem is that neither Python nor the libraries are indefinitely supported. Sooner or later, you will not be able to access security updates and bug fixes. Therefore, you will eventually need to upgrade. Migration can be a hassle if you’re using older versions of Python because you’ll frequently have to deal with more major cross-version changes in both Python and libraries at the same time.  

Hence, a continual, ongoing process of minor upgrades is far safer than a mind-numbing, one-time, massive upgrade every few years. Wait a little while before switching whenever a new major Python version is released, which means that right now, you should really be using 3.10. Do a series of upgrades, starting from 3.8, 3.9, 3.10, and eventually 3.11, if you’re lagging behind. 

And if you are really excited to switch right now, it would probably be wise to wait until your entire toolkit and libraries explicitly support Python 3.11, i.e., mid-December 2022. At that time, you might also have access to the first point release, 3.11.1, in addition to the dependency availability and toolchain support. 

To Conclude 

Are there any out-of-the-ordinary changes in Python 3.11 that completely changes the landscape of this programming language? Not really; however, there have been some notable updates that make Python 22% faster on average and more user-friendly. It’s just that with this release, one of our favorite languages has been made better by these small yet vital changes.  

Happy Pythoning!  


These articles might also be interesting:

Sarrah Pitaliya

Sarrah Pitaliya is a coffee connoisseur and fitness freak. At work, she leads the digital marketing team from the frontline as Digital Marketing Head at Radixweb, a software and web application development company. Her drive to add value to every project she takes up has helped her in creating a massive data bank of the what’s and what not’s of the digital marketing sphere. She writes with a view to share her experience and provide insight to all the finer nuances of digital as a concept.

By Sarrah Pitaliya

Recent Posts