Is this considered black magic?

Terry Reedy tjreedy at
Sun Nov 11 16:20:21 CET 2001

"Laura Creighton" <lac at> wrote in message
news:mailman.1005485895.7082.python-list at
> I want to do something which is conceptually very simple.  Given a
list of
> objects, call make everybody call the same method you want to run.
> of like apply(), but for methods.
> This is what I came up with:
> ...
> def foreach(name_key, object_list, *args):
>     print 'foreach: args are ' + `args`
>     for object in object_list:
>         try:
>             object.__class__.__dict__[name_key](object, *args)
>         except KeyError:
>             pass
> But is it white, grey, or black magic?
>  Is there a better way I should have  done?

try:  getattr(object, name_key)(*args)

>  Suggestions on what to name the function gratefully welcomed.

methmap or mapmeth

Terry J. Reedy

More information about the Python-list mailing list