Created
January 13, 2022 19:12
-
-
Save RahulDas-dev/b99e0616e5fbae96eda4afb54b099f8c to your computer and use it in GitHub Desktop.
Dataclass with inheritanceExample.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from dataclasses import dataclass, field | |
| from pprint import pprint | |
| import inspect | |
| @dataclass(frozen=True, init= False) | |
| class Dimension: | |
| height: float | |
| width: float | |
| depth: float | |
| def __init__(self,height,width,depth ): | |
| object.__setattr__(self, "height", int(round(height))) | |
| object.__setattr__(self, "width", int(round(width))) | |
| object.__setattr__(self, "depth", int(round(depth))) | |
| @property | |
| def volume(self): | |
| return self.height *self.width*self.depth | |
| @property | |
| def area(self): | |
| return self.width*self.depth | |
| @dataclass(frozen=True) | |
| class Item(Dimension): | |
| value:float | |
| d1 = Dimension(10,25.6,89.6) | |
| pprint(d1) | |
| print(d1.volume, d1.area, d1.height) | |
| i1= Item(10,25.6,89.6, 552.6) | |
| pprint(i1) | |
| print(i1.volume, i1.area, i1.height, i1.value ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment