I am trying to get property/setter methods described in the parent class to work when inherited into a child class. I am able to get it to work when I directly assign the call from the parent class as shown below. In my case though, I need to wrap the same call inside a method so that I can make some calculations before I call the parent class. I could get it to work by using fget method and passing an object to it, but I would like the former method to work.
I looked up the issue on StackOverflow and found some methods mentioned that involved using super(), which I tried, but it did not work.
Any help getting this inherited property method to work inside a child class is appreciated!
class A(object):
""" Base Class """
def __init__(self, n):
self.n = n
@staticmethod
def slices():
def get_slices(self):
return self.n
def set_slices(self, n):
self.n = n
return property(get_slices, set_slices)
class B(A):
def __init__(self, n):
self.n = n
his_slices = A.slices() # <--- works well
def her_slices(self): # <--- does not work
return A.slices()
if __name__ == "__main__":
b = B(100)
# This works
print("%d slices" % b.his_slices)
# 100 slices
# This fails
print("%d slices" % b.her_slices)
# TypeError: %d format: a number is required, not method
# Can be made to work like this, but I want above to work
ss = b.her_slices()
print("%d slices" % ss.fget(b))
# 100 slices