Is this considered black magic?
Just van Rossum
just at letterror.com
Sun Nov 11 22:05:21 CET 2001
Laura Creighton wrote:
> Okay, new version:
> def foreach(object_list, method_name, *args):
> for object in object_list:
> method = getattr(object, method_name)
> except AttributeError:
> if callable(method):
> Did I miss something?
One slight improvement that I didn't think of before:
def foreach(object_list, method_name, *args):
for object in object_list:
method = getattr(object, method_name, None)
if method is not None and callable(method):
I think three-arg getattr() is faster than catching an exception.
(I would leave out the callable() test, but then again, I don't
know your exact needs.)
More information about the Python-list