Created
November 5, 2021 16:49
-
-
Save treyhunner/19895add83c5cd762d18e6721a2473bb to your computer and use it in GitHub Desktop.
Demo of property getter/setter in Python
This file contains 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
class Rectangle: | |
def __init__(self, width, height): | |
self.width, self.height = width, height | |
def __repr__(self): | |
return f"Rectangle({self.width}, {self.height})" | |
@property | |
def area(self): | |
return self.width * self.height | |
@area.setter | |
def area(self, area): | |
self.width *= area/self.area |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment