Reputation: 245
I am trying to run 3 different functions that has the form {}.result(). How can I use the getarr or another function so that the all 3 functions are ran within the for loop.
values = ["RSI", "MACD","ROCR"]
for k in values:
result= getattr(k).result()
Runs:
RSI.result()
MACD.result()
ROCR.result()
Upvotes: 1
Views: 66
Reputation: 3565
Rather than putting the names of the objects in a list, why don't you put the actual object in a list (actually a tuple here)?
for i in (RSI, MACD, ROCR):
print(i.result())
Upvotes: 2
Reputation: 1
class Values:
RSI = 1
MACD = 2
ROCR = 3
values = Values()
names = ["RSI","MACD","ROCR"]
for i in names:
print(getattr(values, i))
OUTPUT: 1 2 3
You will not be able to call getattr on your list as getattr gets a named attribute from your object which is equivalentof values.RSI of the code definition above. Otherwise you will get TypeError: getattr(): attribute name must be string
Upvotes: 0