Python is one of the most popular programming languages out there. It is frequently updated with new features and bug-fixes. In this article, we will list the new changes in Python 3.7 and understand what it brings to the table.
Python 3.7 is currently in beta. The release aims to make complex tasks simpler. Many new changes are made to the Python language including data classes, development mode and so on. So, what has changed or added? Let’s get started.
Generator Exception Handling
The first change that we are going to discuss is how Python has improved their generator exception handling. With the 3.7 release, it will become easier to debug the StopIteration exception.
Until now, the StopIteration exception is raised when other problems are encountered. This lead to more bugs as it became hard for developers to find the actual cause of the exception. In PEP 479, they have re-designed the exception handles so that whenever a StopIteration exception occurs, a RuntimeError exception is thrown. This will ensure that the error doesn’t stack up and can be pointed out and fixed immediately.
Python Data Classes
Another big change in the Python 3.7 is the easy way to initiate data classes. Python is a data structure oriented programming language which offers an easy way to initiate data classes and also associate behavior to the data instances.
The change is related to how you will initiate the data classes. Until now to initiate a simple data class, you need to write a good chunk of boilerplate code. Let’s look at an example.
def __init__(self, gov_id, reg_id, approved = True):
self.gov_id = gov_id;
self.reg_id = reg_id;
self.approved = approved;
With Python 3.7, the above code can be written in a much simple way and it is described in the PEP 557 where they approve the dataclasses module as well.
This is a simple example on how dataclasses module can be used to initiate class.
Python provides an amazing in-build debugger. If you are not comfortable using it, you can always switch to other 3rd party debuggers. The only condition that the 3rd party debuggers need to have is to have a way to talk to the internal debugging API of Python.
In 3.7, there will be a standardized way to call 3rd party debuggers. They have added the function, the breakpoint() through which one can easily call a debugger of choice. The debugger choice needs to be set prior to calling the breakpoint() function. Once set, the 3rd party debugger can easily be called off, and it doesn’t have to be pdb, the Python’s in-built debugger.
In comparison, the earlier version required to write verbose code to call 3rd party debuggers. With breakpoint(), it won’t be required anymore.
Legacy C Locale Coercion – UTF-8 Mode
UTF-8 Mode has always been the best way to handle text and strings. In the earlier version of Python, there was a difference in how the ASCII mode is still used in surround environment locale. This leads to a difference in the default way of handling as strings and ASCII as you might want to use UTF-8.
In Python 3.7, the new UTF-8 mode can be forced by simply using the -x command line switch. This will let the environment assume that only UTF-8 is set in the locale.
Better time functions
Python is known for its usage in the scientific community. However, until now it lacked the nanosecond precision. With 3.7, a new set of time Python classes will enable values to be returned with nanosecond precision.
The six new nanosecond variants that are added to the time module are as below.
As you can see a simple “ns” prefix is added to the functions which means using the nanosecond variant of the available functions.
Python importlib resources
The importlib module will get an overhaul in the 3.7 Python update. It is the way to read resources and other data files. By using the new importlib module, you can now import data files even when they are in zip format . It now uses abstraction.
There are many new optimization changes in Python 3.7. For example, new opcodes enable method calls to be faster by 20%. Other than opcodes, case-insensitive matching will also be matched up to 20 times faster in the regular expression.
Read More: – Learn Object Oriented Programming in Python 3
Python development mode
With 3.7, a new command switch is now available for Python interpreter. The “-X” command line switch enables the developer mode in the interpreter. It helps developers with their debugging process, but can impact the performance, and hence should only be used during development.
Final Thoughts: –
Python has one of the leading development community out there. With each iteration, it becomes better and enables the developer to focus on the problem rather than the tool they are using to solve it. If you have any question about the Python 3.7, you can comment below and share it with our audience. Also, you can download the Python 3.7 from Python’s pre-release testing version page.