I tried to understand and use the property decorator mainly for making the property read only. I saw that, every time the property is used, it enters the property decorator.
In my example, it enters the decorator three times, once for every print:
class PropTest:
def __init__(self,x):
self._x = x
@property
def x(self):
return self._x
def check(self):
print(self.x + 1)
print(self.x + 2)
print(self.x + 3)
t = PropTest(3)
t.check()
If I define the property in __init__
, it will enter it only once.
class PropTest:
def __init__(self,x):
self.x = x
def check(self):
print(self.x + 1)
print(self.x + 2)
print(self.x + 3)
t = PropTest(3)
t.check()
Am I using the decorator in a wrong way? Is there any performance issues when I am using it?
L.E. :
This is how I try to use property decorator. I don't want to read the excel each time the property is used and I want the dataframe to be read only.
@property
def rules_df(self):
"""
:return: A dataframe that contains the rules for labels
"""
self._rules_df = pd.read_excel(self.cfg_file)
return self._rules_df