Created
March 13, 2018 21:49
-
-
Save ajtritt/3e4f3cc66b5de5c34cee56efbec88d30 to your computer and use it in GitHub Desktop.
demonstrate override property
This file contains hidden or 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 MyClass(object): | |
def __init__(self, p): | |
self.__x = p | |
@property | |
def x(self): | |
return self.__x | |
@x.setter | |
def x(self, p): | |
self.__x = p | |
class MySubClass(MyClass): | |
def __init__(self, p): | |
self.__x = p | |
@property | |
def x(self): | |
return self.__x | |
foo = MyClass(10) | |
print('Should be 10:', foo.x) | |
foo.x = 20 | |
print('Should be 20:', foo.x) | |
bar = MySubClass(30) | |
print('Should be 30:', bar.x) | |
print("Setting x, should raise AttributeError") | |
bar.x = 20 | |
#print('Should be 20:', bar.x) | |
mamelara
commented
Mar 13, 2018
•
Tried my best to mimic what is happening. I basically don't want to change the interface because that is being used a lot.
And let's just say that f_inteface creates a lot of objects through some important methods. So subclass MyClass and replacing it in f_interface is not as easy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment