iteration over methods
Bengt Richter
bokr at oz.net
Tue Jan 7 12:32:59 EST 2003
On Tue, 7 Jan 2003 08:25:20 -0800, "Kevin Altis" <altis at semi-retired.com> wrote:
[...]
>It would probably be wise to add a check to make sure the item is callable.
>
>class Someclass:
> def __init__(self):
> pass
> def method1(self):
> print 'Hi from method1'
> def method2(self):
> print 'Hi from method2'
> def runallmethods(self):
> # for name, func in self.__class__.__dict__.items():
> for name in dir(self):
> if name[:6] == 'method':
> f = getattr(self, name)
> if callable(f):
> f()
Good point. I took the 'method' prefix as a quasi-guarantee of that,
though of course it isn't a real guarantee. OTOH, if one is expecting
everything starting with 'method' to be of one's own making, one might
like some notification of a non-callable's being found, rather than
silently skipping it.
Regards,
Bengt Richter
More information about the Python-list
mailing list