[Tutor] Two problems related to dispathing.

Kent Johnson kent37 at tds.net
Tue Jul 28 19:47:52 CEST 2009


On Tue, Jul 28, 2009 at 1:22 PM, Mac Ryan<quasipedia at gmail.com> wrote:

> def invokeAll(method, data):
>    """This function is a dispatcher: it invokes all the content types
>    of the application calling 'contenttype.method(data)'"""
>    return [getattr(content, method)(data) for content in contents]

In Python 2.6 you can use operator.methodcaller() to do this:
def invokeAll(method, data):
  caller = operator.methodcaller(method, data)
  return map(caller, contents)

> mycontent = ContentA()
> contents.append(mycontent)

This is not so bad. You could also make a class member of the base
class that keeps track of instances:
class ContentA(object):
  contents = []
  def __init__(self):
    contents.append(self)

Kent


More information about the Tutor mailing list