Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Python 3.7: Gains That Eliminate Pains

DZone's Guide to

Python 3.7: Gains That Eliminate Pains

With Python 3.7 having been recently released, we take a look at what makes it so great and what it brings to the table.

· Open Source Zone ·
Free Resource

Are you looking for ways to manage your open source risk? We can help. Learn more.

In development for 10 months, Python 3.7 has just been released. For long time Python users, it’s a welcome addition, providing many quality of life enhancements that will make writing Python applications quicker and easier. For newcomers, Python 3.7’s optimizations and polish go a long way to justifying the fact that Python is the most popular programming language on the planet right now.

Here are some of the key enhancements it includes.

Data Classes

dataclass helps to simplify the creation of classes, while making them more powerful. All you need to do is declare some fields or attributes with initial values, and the system will automatically create them for you, along with a set of comparison functions (like equals, less than, greater than, etc) that would previously need to be manually implemented.

Previously:

class Point:
    def __init__(self, x, y, z=0.0):
        self.x = x
        self.y = y
        self.z = z

Python 3.7:

@dataclass
class Point:
    x: float
    y: float
    z: float = 0.0


Debugging

breakpoint() has been introduced as a global function just like  print() , allowing you to set a breakpoint so you can easily drop into the debugger and step through your program.

Previously to run the Python debugging library (pdb) you would type:

Import pdb; pdb.set_trace()

Python 3.7:

breakpoint()

So now there’s no need to import pdb, but breakpoint is also more flexible, allowing you to take advantage of any debugging library – and even switch between them – without the need to rewrite your debugging code.

Polish

Two examples of the kind of polish that 3.7 brings to the table are the facts that now the order of dictionaries is guaranteed, and the typing system has been stabilized.

With ordering, previously when you called an index of values, the system would return the values in random order, forcing you to add code to reorder them properly. Now items in dictionaries are iterated over in the same order they were inserted.

Similarly, type hinting and annotations have been in constant development since the first release of Python 3, but 3.7 adds better performance through core support. For example, previously using type hints was painful due to the fact that it loaded the extremely slow type module. Now hints are part of the Python core and won’t slow you down.

Speed

Lastly, Python has always been stigmatized as a slow running language. Python 3.7 takes giant strides toward optimizing for speed, with significant improvements across the board, such as:

  • Importing a typing module is 7x faster
  • Calling methods is 20% faster
  • Startup time is 10-30% faster

As always, ActiveState will be creating a Python 3.7 build when it becomes mature, which generally coincides with the first maintenance release.

Software composition Analysis for DevSecOps. Start finding vulnerabilities in your open source components today.

Topics:
python ,release ,features ,code ,performance ,speed ,open source

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}