Let's say I have a class like this:
class calculations():
def __init__(self,x,y):
self.x = x
self.y = y
def calc_sum(self):
return self.x+self.y
def calc_dif(self):
return self.x-self.y
I was wondering, can I somehow loop through methods in a class in a for loop? For example if I input 10
and 10
it would return both calc_sum
and calc_dif
.
I've tried like so:
for item in calculations(10,10):
print(item)
TypeError: 'calculations' object is not iterable
And I would expect 20
and 0
.
Does that mean that classes are not iterable or is there a way to iter through them?
UPDATE
When I run:
for item in list(dir(calculations)[-2:]):
print(item)
I get
calc_dif
calc_sum
And now I want to pass 10
and 10
to both of these methods and instead of getting the names of the methods to get the results they return. I now understand that this is not a good practice but I am just curious how would I achieve this.