Created
August 25, 2016 20:59
-
-
Save motivic/642387ba08b3c5630a3b4ea18de68e4c to your computer and use it in GitHub Desktop.
Properties and Inheritance in Python
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
| # Property and inheritance | |
| # http://stackoverflow.com/questions/1021464/how-to-call-a-property-of-the-base-class-if-this-property-is-being-overwritten-i | |
| class Foo(object): | |
| def __init__(self): | |
| self._x = 'Foo' | |
| @property | |
| def x(self): | |
| return self._x | |
| @x.setter | |
| def x(self, value): | |
| print(value) | |
| class FooBar(Foo): | |
| @property | |
| def x(self): | |
| return super(FooBar, self).x | |
| @x.setter | |
| def x(self, value): | |
| self._x = value | |
| # Call setter in base class | |
| super(FooBar, self.__class__).x.fset(self, value) | |
| # super(FooBar, self).x.fset(self, value) returns an error | |
| # super(FooBar, self.__class__).x | |
| # refers to the property x | |
| # whereas | |
| # super(FooBar, self).x | |
| # calls the getter of x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment