Is there a way to use methods within a loop in Python? Something like the following:
obj=SomePythonObject()
list_of_methods=dir(obj)
for i in list_of_methods:
try:
print obj.i()
except:
print i,'failed'
Is there a way to use methods within a loop in Python? Something like the following:
obj=SomePythonObject()
list_of_methods=dir(obj)
for i in list_of_methods:
try:
print obj.i()
except:
print i,'failed'
Yes that's possible, use callable and getattr:
obj=SomePythonObject()
list_of_methods=dir(obj)
for i in list_of_methods:
try:
item = getattr(obj, i, None)
if callable(item):
item()
except:
print i,'failed'