Skip to content

Instantly share code, notes, and snippets.

@hakandilek
Last active October 9, 2024 08:42
Show Gist options
  • Save hakandilek/aefba1cb84fa23a151356dcc01961da6 to your computer and use it in GitHub Desktop.
Save hakandilek/aefba1cb84fa23a151356dcc01961da6 to your computer and use it in GitHub Desktop.
Python property overrides
class A:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
@property
def id(self):
return "some id"
class B(A):
def __init__(self, name):
super().__init__(name)
@property
def name(self):
n = A.name.fget(self)
return f"super {n}"
@name.setter
def name(self, name):
A.name.fset(self, name)
if __name__ == '__main__':
a = A("a from A")
print(a.name)
b = B("b from B")
print(b.name)
b.name = "new b from B"
print(b.name)
a from A
super b from B
super new b from B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment