Skip to content

Instantly share code, notes, and snippets.

@treyhunner
Created November 5, 2021 16:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treyhunner/19895add83c5cd762d18e6721a2473bb to your computer and use it in GitHub Desktop.
Save treyhunner/19895add83c5cd762d18e6721a2473bb to your computer and use it in GitHub Desktop.
Demo of property getter/setter in Python
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