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
Interesting ideas for using AI in e-commerce

Idego

Substantive support - Julianna Sykutera

Interesting ideas for using AI in e-commerce

In today’s world, when you run a company, your customers require your service availability all the time – 24/7.  Not all customers are so demanding, but a considerable group surely wants you to offer professional support at any moment. With traditional solutions, providing 24 hours per day customer service is very expensive. Fortunately, you can use AI-based solutions for e-commerce. […]

Should you use Angular for developing a business solution?

Choosing the best tech stack for your company’s application is not easy – especially if you’re not tech-savvy. How can you make an informed choice between so many popular programming languages and their frameworks? We would like to tell you more about Angular and its application in business projects.  Angular has now been on the market for quite some time […]

Should you use Angular for developing a business solution?

Idego

Substantive support - Julianna Sykutera

Main differences between deep learning and machine learning

Idego

Substantive support - Julianna Sykutera

Main differences between deep learning and machine learning

Since the moment artificial intelligence first appeared, many new business and technological solutions have been invented. Creating them, though, would not be possible if not for the development of machine learning – and later deep learning – which enabled machines to learn how to process user requests similarly the human would do.  Machine learning, which was invented in 1980 (referred […]

What are the most popular JavaScript frameworks?

JavaScript and frameworks created in that language can be used for both backend and frontend parts of an application. Better knowledge about available technologies will help you make an informed decision when it comes to choosing a tech stack for your next project. JavaScript has a considerable number of backend and frontend frameworks that can be used for mobile and […]

What are the most popular JavaScript frameworks?

Idego

Substantive support - Julianna Sykutera

Get a free estimation

Need a successful project?