[Tutor] Two problems related to dispathing.

Mac Ryan quasipedia at gmail.com
Tue Jul 28 19:22:11 CEST 2009


Hello,

	I'm working on an application in which I need the main loop to call the
same method on a number of objects. I came up with this solution that
seem to work, but I was wondering if there is a better / more pythonic
solution to the problem.

<code>
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]
</code>

I am also unsatisfied by how I generate the content list I use in the
above function. In fact, as I have the need to access the object both
individually and with the dispatcher, I am doing something like this
every time I instantiate a new object: 

<code>
mycontent = ContentA()
contents.append(mycontent)
</code>

...but although I "feel" this is silly, I can't imagine how to get it
better (i.e. a single call that will make available the object with his
unique name and append it to the list of contents.

Thank you in advance for your input,
Mac.



More information about the Tutor mailing list