Data Classes in Python

As of Python 3.7, we will be able to mark the class we define with the @dataclass decorator. Doing so, the newly defined class will be a data class (similarly to Data Class in Kotlin). As with Kotlin, the main outcome of having our class defined as a data class is having auto-generated methods, such as __init__, __repr__ and few others.

from dataclasses import dataclass

@dataclass()
class X:
a:any = 10
b:int = 2

x = X(2,5)

print(x)

The output of running this code snippet will be the following:

X(a=2, b=5)

You can find detailed information about this new capability at https://www.python.org/dev/peps/pep-0557/.

Share:

banner for the css playlist in hebrew life michael courses for programmers

The First Steps in CSS

Learn CSS using our our videos (in Hebrew) on the CSS (he) playlist on youtube. Do it now. Do it for free.

Good Trainers Collaborate with Others

It is always essential to keep an open mind and learn from others. This applies to everyone, including teachers and especially software development trainers. Software

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Update cookies preferences