Skip to content

Instantly share code, notes, and snippets.

@RahulDas-dev
Created January 13, 2022 19:11
Show Gist options
  • Save RahulDas-dev/666f1aee85ebfde19e901f4457acd997 to your computer and use it in GitHub Desktop.
Save RahulDas-dev/666f1aee85ebfde19e901f4457acd997 to your computer and use it in GitHub Desktop.
Dataclass with init ,field modification while object initialization
@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