Last active
October 9, 2024 08:42
-
-
Save hakandilek/aefba1cb84fa23a151356dcc01961da6 to your computer and use it in GitHub Desktop.
Python property overrides
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 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) |
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
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