Created
January 13, 2022 19:11
-
-
Save RahulDas-dev/666f1aee85ebfde19e901f4457acd997 to your computer and use it in GitHub Desktop.
Dataclass with init ,field modification while object initialization
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
@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 | |
d1 = Dimension(10,25.6,89.6) | |
pprint(d1) | |
print(d1.volume, d1.area, d1.height) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment