iteration over methods
Peter Abel
p-abel at t-online.de
Tue Jan 7 17:26:56 EST 2003
"John Roth" <johnroth at ameritech.net> wrote in message news:<v1lkl079pb5ib6 at news.supernews.com>...
> "Oliver Vecernik" <vecernik at aon.at> wrote in message
> news:3E1AB7B3.4050404 at aon.at...
> > Hi!
> >
> > I've got an object with some methods:
> >
> > class Someclass(self):
> > def __init__(self):
> > pass
> > def method1(self):
> > pass
> > def method2(self):
> > pass
> > ...
> > def runallmethods(self):
> > for func in dir(self):
> > if func[0:4] == 'method':
> > apply(self.func) # Does *not* work!
> >
> > I'd like to run all methods named 'method...' in a sequence. Has
> anybody
> > a clue how to achive this?
>
> Look at the "unittest" module. It does exactly this.
>
> John Roth
> >
> > Best Regards,
> > Oliver
> >
Try this
>>> import types
>>> class A:
... def __init__(self):
... pass
... def m1(self):
... print "m1"
... def m2(self):
... print "m2"
... def run_all(self):
... for i in dir(self):
... if type(eval('self.'+i))==types.MethodType and i!='run_all' and
i!='__init__':
... eval('self.'+i)()
...
>>> a=A()
>>> a.run_all()
m1
m2
>>>
More information about the Python-list
mailing list