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/.

Leave a Reply

Your email address will not be published. Required fields are marked *