python properties and inheritance

45,808

Solution 1

I simply prefer to repeat the property() as well as you will repeat the @classmethod decorator when overriding a class method.

While this seems very verbose, at least for Python standards, you may notice:

1) for read only properties, property can be used as a decorator:

class Foo(object):
    @property
    def age(self):
        return 11

class Bar(Foo):
    @property
    def age(self):
        return 44

2) in Python 2.6, properties grew a pair of methods setter and deleter which can be used to apply to general properties the shortcut already available for read-only ones:

class C(object):
    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

Solution 2

I don't agree that the chosen answer is the ideal way to allow for overriding the property methods. If you expect the getters and setters to be overridden, then you can use lambda to provide access to self, with something like lambda self: self.<property func>.

This works (at least) for Python versions 2.4 to 3.6.

If anyone knows a way to do this with by using property as a decorator instead of as a direct property() call, I'd like to hear it!

Example:

class Foo(object):
    def _get_meow(self):
        return self._meow + ' from a Foo'
    def _set_meow(self, value):
        self._meow = value
    meow = property(fget=lambda self: self._get_meow(),
                    fset=lambda self, value: self._set_meow(value))

This way, an override can be easily performed:

class Bar(Foo):
    def _get_meow(self):
        return super(Bar, self)._get_meow() + ', altered by a Bar'

so that:

>>> foo = Foo()
>>> bar = Bar()
>>> foo.meow, bar.meow = "meow", "meow"
>>> foo.meow
"meow from a Foo"
>>> bar.meow
"meow from a Foo, altered by a Bar"

I discovered this on geek at play.

Solution 3

Another way to do it, without having to create any additional classes. I've added a set method to show what you do if you only override one of the two:

class Foo(object):
    def _get_age(self):
        return 11

    def _set_age(self, age):
        self._age = age

    age = property(_get_age, _set_age)


class Bar(Foo):
    def _get_age(self):
        return 44

    age = property(_get_age, Foo._set_age)

This is a pretty contrived example, but you should get the idea.

Solution 4

Yes, this is the way to do it; the property declaration executes at the time the parent class' definition is executed, which means it can only "see" the versions of the methods which exist on the parent class. So when you redefine one or more of those methods on a child class, you need to re-declare the property using the child class' version of the method(s).

Solution 5

A possible workaround might look like:

class Foo:
    def __init__(self, age):
        self.age = age

    @property
    def age(self):
        print('Foo: getting age')
        return self._age

    @age.setter
    def age(self, value):
        print('Foo: setting age')
        self._age = value


class Bar(Foo):
    def __init__(self, age):
        self.age = age

    @property
    def age(self):
        return super().age

    @age.setter
    def age(self, value):
        super(Bar, Bar).age.__set__(self, value)

if __name__ == '__main__':
    f = Foo(11)
    print(f.age)
    b = Bar(44)
    print(b.age)

It prints

Foo: setting age
Foo: getting age
11
Foo: setting age
Foo: getting age
44

Got the idea from "Python Cookbook" by David Beazley & Brian K. Jones. Using Python 3.5.3 on Debian GNU/Linux 9.11 (stretch)

Share:
45,808
Peter Hoffmann
Author by

Peter Hoffmann

Director Data Engineering at Blue Yonder Follow me on twitter/peterhoffmann

Updated on July 08, 2022

Comments

  • Peter Hoffmann
    Peter Hoffmann almost 2 years

    I have a base class with a property which (the get method) I want to overwrite in the subclass. My first thought was something like:

    class Foo(object):
        def _get_age(self):
            return 11
    
        age = property(_get_age)
    
    
    class Bar(Foo):
        def _get_age(self):
            return 44
    

    This does not work (subclass bar.age returns 11). I found a solution with an lambda expression which works:

    age = property(lambda self: self._get_age())
    

    So is this the right solution for using properties and overwrite them in a subclass, or are there other preferred ways to do this?