Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a property of the base class if this property is being overwritten in the derived class?

I'm changing some classes of mine from an extensive use of getters and setters to a more pythonic use of properties.

But now I'm stuck because some of my previous getters or setters would call the corresponding method of the base class, and then perform something else. But how can this be accomplished with properties? How to call the property getter or setter in the parent class?

Of course just calling the attribute itself gives infinite recursion.

class Foo(object):      @property     def bar(self):         return 5      @bar.setter     def bar(self, a):         print a  class FooBar(Foo):      @property     def bar(self):         # return the same value         # as in the base class         return self.bar # --> recursion!      @bar.setter     def bar(self, c):         # perform the same action         # as in the base class         self.bar = c    # --> recursion!         # then do something else         print 'something else'  fb = FooBar() fb.bar = 7 
like image 425
UncleZeiv Avatar asked Jun 20 '09 11:06

UncleZeiv


People also ask

Can base class access derived class properties?

// As base-class pointer cannot access the derived class variable.

Can properties be override?

An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property. Beginning with C# 9.0, read-only overriding properties support covariant return types. The overridden property must be virtual , abstract , or override .

Can you override properties in C#?

Overriding Properties in C#We can also override the property of a parent class from its child class similar to a method. Like methods, we need to use virtual keyword with the property in the parent class and override keyword with the porperty in the child class.

How do you call a Python property?

You can create a property by calling property() with an appropriate set of arguments and assigning its return value to a class attribute. All the arguments to property() are optional. However, you typically provide at least a setter function.


2 Answers

You might think you could call the base class function which is called by property:

class FooBar(Foo):      @property     def bar(self):         # return the same value         # as in the base class         return Foo.bar(self) 

Though this is the most obvious thing to try I think - it does not work because bar is a property, not a callable.

But a property is just an object, with a getter method to find the corresponding attribute:

class FooBar(Foo):      @property     def bar(self):         # return the same value         # as in the base class         return Foo.bar.fget(self) 
like image 69
David Cournapeau Avatar answered Sep 20 '22 21:09

David Cournapeau


super() should do the trick:

return super().bar 

In Python 2.x you need to use the more verbose syntax:

return super(FooBar, self).bar 
like image 25
Pankrat Avatar answered Sep 20 '22 21:09

Pankrat