Skip to content

Instantly share code, notes, and snippets.

@motivic
Created August 25, 2016 20:59
Show Gist options
  • Select an option

  • Save motivic/642387ba08b3c5630a3b4ea18de68e4c to your computer and use it in GitHub Desktop.

Select an option

Save motivic/642387ba08b3c5630a3b4ea18de68e4c to your computer and use it in GitHub Desktop.
Properties and Inheritance in Python
# 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