I have the following code.
class DobleTSim():
def __init__(self, bf, hw, tf, tw):
self.bf = bf
self.hw = hw
self.tf = tf
self.tw = tw
def m_in_maj(self):
print('foo')
return 2 * (self.bf * self.tf * (self.tf / 2 + self.hw / 2))
def m_est_maj(self):
return self.m_in_maj() / ((self.hw + 2 * self.tf) / 2)
A = DobleTSim(200, 500, 12, 12)
print(A.m_in_maj(), A.m_est_maj())
When I execute the code, the output is :
foo
foo
1228800.0 4690.076335877862
How can I avoid execute the method "m_in_maj" twice?
-----EDIT-----
Another solution can be the use of a property and the lru_cache decorator. Is there a disadvantage when using this?.
import functools
class DobleTSim():
def __init__(self, bf, hw, tf, tw):
self.bf = bf
self.hw = hw
self.tf = tf
self.tw = tw
@property
@functools.lru_cache()
def m_in_maj(self):
print('foo')
self.a = 2 * (self.bf * self.tf * (self.tf / 2 + self.hw / 2))
return self.a
def m_est_maj(self):
return self.m_in_maj / ((self.hw + 2 * self.tf) / 2)