Python

PEP 557 — Data Classes Python-Version: 3.7

A class decorator is provided which inspects a class definition for variables with type annotations as defined in PEP 526, “Syntax for Variable Annotations”. In this document, such variables are called fields. Using these fields, the decorator adds generated method definitions to the class to support instance initialization, a repr, comparison methods, and optionally other methods as described in the Specification section. Such a class is called a Data Class, but there’s really nothing special about the class: the decorator adds generated methods to the class and returns the same class it was given.

A class decorator is provided which inspects a class definition for variables with type annotations as defined in PEP 526, “Syntax for Variable Annotations”. In this document, such variables are called fields. Using these fields, the decorator adds generated method definitions to the class to support instance initialization, a repr, comparison methods, and optionally other methods as described in the Specification section. Such a class is called a Data Class, but there’s really nothing special about the class: the decorator adds generated methods to the class and returns the same class it was given. [PEP 557]

Let’t make a class and test inheritance.

So here we have imported dataclass and fields (__init__), then we made a machine class and a pump class. The pump class uses the machine class fields in this example.

Then we print the representation of the objects.

If we compare with how it used to be and still is used in previous Python version:

Comments Off on PEP 557 — Data Classes Python-Version: 3.7