In this post, we will discuss how DataClasses behave when inherited. Though they make their own constructors, DataClasses behave pretty much the same way as normal classes do when inherited.
Few points from above code:
Articleis subclassed by
- Both SuperClass and SubClass are DataClasses – although super-class or sub-class being a normal class is also possible. When a DataClass inherits a normal class, the __init__() from the super-class is overidden in sub-class.
authorin GfgArticle overrides the same in Article – As the basic concept of inheritance, the value for its assignment is first looked in the sub-class and followed up the tree in super-class.
Behaviour of __init__() of GfgArticle:
- If __init__() is not explicitly provided, the default __init__() expects attributes of super-class (Article) followed by attributes of sub-class as parameters.
- If __init__() is explicitly provided, it should somehow initialize all it’s own attributes as well as those in the super-class (Article).
GfgArticle(title: str, content: str, author: str, language: str, upvotes: int = 0)
Note: The signature expects
language in-spite of opposite order of declaration in GfgArticle. This comes from the fact that attributes are scanned top to bottom from super-class followed by sub-class. So
author is first scanned in Article then
language is scanned in GfgArticle.
GfgArticle(title=’DataClass’, content=’SuperCool DataStructure’, author=’vibhu4agarwal’, language=’Python3′, upvotes=0)
GfgArticle(title=’DataClass’, content=’Inheritance Concepts’, author=’vibhu4agarwal’, language=’Python3′, upvotes=0)
- Parameters requirement in __init__() can be adjusted according to the need as long as it has some way of initializing all the attributes.
- Order of initialization doesn’t matter.
languageis initialized before
contentis initialized at last and it still works.
- Data Classes in Python | An Introduction
- Data Classes in Python | Set 5 (post-init)
- Data Classes in Python | Set 6 (interconversion to and from other datatypes)
- Data Classes in Python | Set 2 (Decorator Parameters)
- Data Classes in Python | Set 3 (dataclass fields)
- Inheritance in Python | Set 2
- Inheritance in Python
- Inheritance in Python Inner Class
- Types of inheritance Python
- Multiple inheritance in Python
- Python | super() in single inheritance
- Python | super() function with multilevel inheritance
- Method resolution order in Python Inheritance
- OOP in Python | Set 3 (Inheritance, examples of object, issubclass and super)
- Python Classes and Objects
- Abstract Classes in Python
- Create Classes Dynamically in Python
- How to Dynamically Load Modules or Classes in Python
- Python - Convert Tick-by-Tick data into OHLC (Open-High-Low-Close) Data
- Exporting PDF Data using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.