I'm learning the python @property
decorator. I am stuck and couldn't able to find the solution. I checked this and this one in SO, but my question is different.
Here, I have created a Base
class in which I made the function fun_name
as an attribute to this Base
class using @property
decorator. Everything works as expected.
I want to know about the behavior of this with inheritance.
For this, I have created another class Derived
which is derived from the Base
class. Here in the Derived
class , I am trying to access the property attribute function fun_name
using the Derived
class member function sample_fun
.
Accessing the fun_name
attribute directly using the Base
class object (obj1
) works as expected. But I am confused with the behavior when I am calling the same using the Derived
class member function sample_fun
using the obj2
object.
class Base:
def __init__(self,name):
self.name = name
@property
def fun_name(self):
print("Entered the getter fun_name")
return "Getter-> Hello {}".format(self.name)
@fun_name.setter
def fun_name(self,str):
self.name = str
print("Setter-> Hello {}".format(self.name))
def fun_name1(self):
print("Hello from fun_name1 {}".format(self.name))
class Derived(Base):
def sample_fun(self):
print("Calling base call fun directly")
Base.fun_name1(self)
print("It works")
print("Calling base attrib directly")
Base.fun_name #Here Why it is not entering into the fun_name ?
print("It prints here but not entering into the fun_name")
obj1 = Base("Joe")
obj1.fun_name
obj1.fun_name = "Jim"
obj1.fun_name
obj2 = Derived("RAJ")
obj2.sample_fun()
Console output:
Entered the getter fun_name
Setter-> Hello Jim
Entered the getter fun_name
Calling base call fun directly
Hello from fun_name1 RAJ
It works
Calling base attrib directly
It prints here but not entering into the fun_name
As you can see when I am directly accessing it using the Base
class object (obj1
) , it enters into the function.
But the same behavior is not happening with the Derived
class member function. Exactly at the place Base.fun_name
in the sample_fun
method.
Am I missing some important concept here?