What's new in Python 3.7?



The latest version of Python has diverse new features and tweaks, all of which can be found in Python 3.7’s official documentation.

However, let’s have a look at the most essential upgrades:

svg viewer

Built-in breakpoint() function

Debugging is an important part of programming, and Python 3.7 makes it more flexible and intuitive. The new breakpoint() function replaces the older, more cumbersome syntax that is associated with previous versions of Python.

# Old breakpoint syntax:
def divide(a, b):
    import pdb; pdb.set_trace()
    return a / b

# New breakpoint syntax:
def divide(a, b):
    breakpoint()
    return a / b

Data classes

The new data classes module makes it more convenient to create classes as special methods like .__init__(), .__repr__(), .__eq__(), and several more are added automatically. Consider the following example that highlights the usage of a data class:

from dataclasses import dataclass, field
@dataclass(order=True)
class Employee:
name: str
age: int
designation: float = field(repr=False, compare=False)
minimumSalary: float = 5000
def somefunc(self):
pass
# some stuff

Increased time precision

The time module has been upgraded such that each time function returns the number of nanoseconds as an int, rather than as a float. These new functions are resolved three times faster than the older functions.

The accuracy is also increased because floating-point numbers are naturally inaccurate due to limitations in storing bits.

Performance enhancement to annotations

Type hints and annotations have been around since Python 3.5. However, they lacked performance and stability; Python 3.7 enhances their performance, improves core support, and allows forward references as well.

Ordered dictionaries

Dictionaries are now guaranteed to preserve insertion order. The normal dict has replaced the older collections.OrderedDict in most cases. The dict feature was informally implemented in Python 3.6, but it is now officially documented in Python 3.7.

Miscellaneous optimizations

Python 3.7 is significantly faster than its predecessor, speed-ups include:

  • There is less overhead when calling many methods in the standard library.
  • Method calls are generally up to 20% faster.
  • The startup time of Python, itself, has been reduced by 10-30%.
  • Importing the typing module is seven times faster.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved