In this post, I will explain improvements done in Core Python version 3.7. Below is the outline of features covered in this post.
Hash-based Python object file
Breakpoint is an extremely important tool for debugging. Since I started
learning Python, I am using the same API for putting breakpoints. With this
breakpoint() is introduced as a built-in function. Because it is
in a built-in scope, you don’t have to import it from any module. You can call
this function to put breakpoints in your code. This approach is handier than
Code used in above example
for i in range(100): if i == 10: breakpoint() else: print(i)
There wasn’t any handy option to disable or enable existing breakpoints with a
single flag. But with this release, you can certainly reduce your pain by using
PYTHONBREAKPOINT environment variable. You can disable all breakpoints in
your code by setting the environment variable
I advise putting “PYTHONBREAKPOINT=0” in your production environment to avoid unwanted pausing at forgotten breakpoints
You can pipe the output of Standard Output Stream (stdout) and Standard Error
Stream (stderr) by enabling
capture_output parameter of
You should note that it is an improvement over piping the stream manually. For
subprocess.run(["ls", "-l", "/var"], stdout=subprocess.PIPE,
stderr=subprocess.PIPE) was the previous approach to capture the output of
The new class level decorator
@dataclass introduced with the
dataclasses module. Python is well-known for achieving more by writing
less. It seems that this module will receive more updates in future which can be
applied to reduce significant line of code. Basic understanding of Typehints is
expected to understand this feature.
When you wrap your class with the
@dataclass decorator, the decorator will
put obvious constructor code for you. Additionally, it defines a behaviour for
Below is the code before introducing a
class Point: def __init__(self, x, y): self.x = x self.y = y
After wrapping with
@dataclass decorator it reduces to below code
from dataclasses import dataclass @dataclass class Point: x: float y: float
The namedtuples are a very helpful data structure, yet I found it is less known amongst developers. With this release, you can set default values to argument variables.
Note: Default arguments will be assigned from left to right. In the above example, default value
2 will be assigned to variable
Below is the code used in the example
from collections import namedtuple Point = namedtuple("Point", ["x", "y"], defaults=[2,]) p = Point(1) print(p)
.pyc are object files generated everytime you change your code file (.py). It is a collection of meta-data created by an interpreter for an executed code. The interpreter will use this data when you re-execute this code next time. Present approach to identify an outdated object file is done by comparing meta fields of source code file like last edited date. With this release, that identification process is improved by comparing files using a hash-based approach. The hash-based approach is quick and consistent across various platforms than comparing last edited dates. This improvement is considered unstable. Core python will continue with the metadata approach and slowly migrate to the hash-based approach.
breakpoint()will put a breakpoint in your code.
Disable all breakpoints in your code by setting an environment variable
subprocess.run([...], capture_output=True)will capture the output of
Class level decorator
@dataclasswill define default logic for constructor function. It will implement default logic for dunder methods
Namedtuple data structure supports default values to its arguments using
Outdated Python object files (.pyc) are compared using the hash-based approach.
I hope you were able to learn something new by reading this post. If you want to read an in-depth discussion on each feature introduced in Python 3.7, then please read this official post. Happy hacking!