Is this considered black magic?

Terry Reedy tjreedy at home.com
Sun Nov 11 10:20:21 EST 2001


"Laura Creighton" <lac at strakt.com> wrote in message
news:mailman.1005485895.7082.python-list at python.org...
> 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.
Sort
> 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