Is this considered black magic?

Christian Tanzer tanzer at swing.co.at
Mon Nov 12 09:37:14 CET 2001


Laura Creighton <lac at strakt.com> wrote:

> Okay, new version:
> 
> def foreach(object_list, method_name, *args):
>     for object in object_list:
>         try:
>             method = getattr(object, method_name)
>         except AttributeError:
>             pass
>         else:
>             if callable(method):
>                 method(*args)
> 
> Did I miss something? 

I'd skip the `callable` test. Otherwise you hide cases, where an
object has an attribute `method_name` instead of a method -- but then
maybe that's intentional.

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list