I'm trying to simplify my code to increase productivity and avoid typing errors and Object Oriented Programming (OOP) seems the way to follow. I'm new to OOP and have started to learn about classes and methods in Python. I'm working in a personal project involving consecutive steps to get a final result in which every step depends on the results of previous steps but intermediate results are also useful on its own.
Let's start with the definition of the class:
class MyClass():
def __init__(self, data):
self.var_1 = data[0]
self.var_2 = data[1]
self.var_3 = data[2]
def method_1(self):
self.result_1 = self.var_1 + self.var_2
return self.result_1
def method_2(self):
self.result_2 = self.var_3 * self.result_1
return self.result_2
Next I create the object:
food = [7, 15, 32]
A = MyClass(food)
print(A.method_2)
The result:
<bound method MyClass.method_2 of <__main__.MyClass object at 0x00000249A2C54C48>> instead of the desired 704
I have done my homework and tried several solutions:
First: call every method needed to get result prior to call the desired.
B = MyClass(food) B.method_1() print(B.method_2())
Result:704
Second: put the calls in init to force to evaluate them every time I instantiate the object. Discovered here:Enforce a sequence for calling methods in a class
class MyClass():
def __init__(self, data): self.var_1 = data[0] self.var_2 = data[1] self.var_3 = data[2] MyClass.method_1(self) MyClass.method_2(self) def method_1(self): self.result_1 = self.var_1 + self.var_2 return self.result_1 def method_2(self): self.result_2 = self.var_3 * self.result_1 return self.result_2
Creating and calling gives 704:
C= MyClass(food)
print(C.method_2())
This can be modified using a idea from this thread:Run all functions in class
Defining a new method within the class:
def method_all(self):
MyClass.method_1(self)
MyClass.method_2(self)
Calling before does the trick
E = MyClass(food)
E.method_all()
print(E.method_2())
Is there a preferred method? Another solution that forces to evaluate needed methods before the desired one?