I have
class Foo():
function bar():
pass
function foobar():
pass
Rather than executing each function one by one as follows:
x = Foo()
x.bar()
x.foobar()
is there a built-in way to loop through and execute each function in the sequence in which they are written in the class?
解决方案
No. You can access Foo.__dict__, and call each value in turn (catching errors for non-callable members), but the order is not preserved.
for callable in Foo.__dict__.values():
try:
callable()
except TypeError:
pass
This assumes none of the functions take parameters, as in your example.