back

Your free e-book!

See when it is not worth using Scrum.
"Why Scrum Doesn't Work" Download

5 Cool New Features in Python 3.7!

Python 3.7 was released on June 27, 2018. It came with few interesting features and many performance improvements.  What does exactly the new Python version bring? Is it as cool as we all expected? Let’s see! In short words, you can find there:

  • new breakpoint() function,
  • async / await as reserved keywords,
  • module level __getattr__ and __dir__ method
  • nanosecond resolution in time functions
  • postponed evaluation of type hints (annotations)
  • context variables
  • data classes

In this note I’m going to take deep insight into data classes. In my opinion this is the most important change in our latest Python version.

Software Engineers - development team

Why data classes are great

Data classes allow to write more consistent and meaningful code. As other cool features in Python like e.g. generators, they provide new syntax for describing thinking processes more compactly and with less lines of code. This is what makes Python awesome ( import this ).

Let’s get to the point

Data classes provide new class decorator that can be used with classes to represent data structures. They came to the standard library with a new dataclasses module:

from dataclasses import dataclass

Simple data class

@dataclass
class City:
  citizens: int
  area: float

krakow = City(767, 326)

print(krakow)

> City(citizens=767, area=326)

With this 4-lines class declaration, we get automatically created __init__ and __repr__ function definitions.

Ordered data classes

Morover we can also define ordered data structures and compare if needed:

@dataclass(order=True)
class City:
  name: str = field(compare=False)
  citizens: int
  area: float

zamosc = City('Zamość', 65, 30)
krakow = City('Kraków', 767, 326)
ochock = City('Ochock', 3, 400)

bigger = zamosc if zamosc > krakow else krakow
print(f'Bigger city: {bigger}')
print(sorted([zamosc, krakow, ochock]))

> Bigger city: City(name='Kraków', citizens=767, area=326)
> [City(name='Ochock', citizens=3, area=400), City(name='Zamość', citizens=65, area=30), City(name='Kraków', citizens='767', area=326)]

With this example we’ve just introduced new field function (from dataclasses module). It allows to override default behavior of the class for a single field – in this case we can disable ordering based on city names.

As you can see, we can also sort data objects with sorted. What’s interesting here, is that class atributes ordering matters – in this case citizen is the significant parameter.

Default dataclass parameters

By default, dataclass decorator is set up with given list of parameters:

@dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, f)

They correspond to autogenerated methods like __init__ , __repr__ , __eq__ , __hash__ and ordering helpers ( __lt__ , __le__ , __gt__ , and __ge__ ), so you have control over what’s acutally being generated in specific case. frozen parameter allows to create immutable objects.

Inheritance

Data classes can be inherited by child-classes. Let’s see what is the ordering of the resulted class atributes.

@dataclass
class A:
  x: int = 1
  y: int = 2

@dataclass
class B(A):
  z: int = 3
  x: int = 5

print(A())
print(B())
print(B(0, 1, 2))
> A(x=1, y=2)
> B(x=5, y=2, z=3)
> B(x=0, y=1, z=2)

Field ordering from the base class is preserved.

Last but not least

@dataclass
class Product:
  size: int
  quantity: int
  price: float

p = Product(10, 1, 1.23)
print(asdict(p))
> {'size': 10, 'quantity': 1, 'price': 1.23}

Whoa! We’ve just introduced JSON serialization of Python object with just few lines of code – it wasn’t that trivial before (see this and this). With asdict dataclasses module level function, you can just get dict representation of data structure hold by given data class. Just send it over your favourite API framework.

Conclusion about python 3.7 features

Dataclasses can be very useful improvement to your daily coding style. It’s nice to have separated data model and business logic services, and with our new feature it’s easier to achieve. This new Python module allows to write data stores very explicitly. Even if if it’s not providing any new functional changes, it’s definitely good to adjust your code base. In case of updating from recent versions of Python, it’s not bringing any backwards incompatible changes, cause it’s settled in a brand new module. However, when upgrading to Python 3.7, it can be profitable to refactor existing classes to make use of dataclasses . Just identify your data objects and reduce your lines of code with @dataclass decorator. “Readability counts” – stay tuned!

Reference

https://www.python.org/dev/peps/pep-0557/

https://docs.python.org/3/whatsnew/3.7.html

https://realpython.com/python37-new-features/

https://docs.python.org/3/reference/index.html


cto - Chris Gibas

Free 30-minute consultation with our CTO

Chris Gibas - our CTO will be happy to discuss your project! Let's talk!

More blog posts
Decentralized Finance (DeFi) – what is it?

Oleksandra Bilokrys

Decentralized Finance (DeFi) – what is it?

Decentralized Finance, in short DeFi, is a concept which enables entrepreneurs who choose to take advantage of blockchain technology and traditional financial instruments in a decentralized architecture. Two examples of DeFi applications are Bitcoin and Etherum. They are both controlled by big networks of computers and not central authorities. What does that mean and why is DeFi becoming more and […]

What are smart contracts and what is their potential for business?

Are you interested in blockchain technology, and you are wondering what its business potential is? To understand the possible application in your industry, you first need to learn how it works. One of the most useful tools associated with blockchain is smart contracts. They enable transferring not only fiat currency and cryptocurrencies but also other resources. Smart contracts are applications […]

What are smart contracts and what is their potential for business?

Idego

Substantive support - Oleksandra Bilokrys

How to augment your software development team? Staff augmentation in practice.

Idego

Substantive support - Oleksandra Bilokrys

How to augment your software development team? Staff augmentation in practice.

Do you need high-quality software for your company? In some situations only high-quality, custom solutions can solve certain business problems, but how to develop the right software? No one better than you knows what exactly you need to become more efficient and thus, more competitive. If you do not have enough experienced employees for such a big IT undertaking in […]

Hiring the best machine learning developer – what do you need to know?

Finding and hiring specialists for your IT department is never easy, especially when your company is taking advantage of quite complex IT solutions for business. Do you need to employ the best machine learning developer available on the market? A deep understanding of the IT field – which is a data-based science – is crucial for HR experts and managers […]

Hiring the best machine learning developer – what do you need to know?

Idego

Substantive support - Oleksandra Bilokrys

Get a free estimation

Need a successful project?