Is this considered black magic?
tjreedy at home.com
Sun Nov 11 16:20:21 CET 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
> 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:
> object.__class__.__dict__[name_key](object, *args)
> except KeyError:
> 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