> 
> Yes, this is what I'd recommend. A handler for an ObjectAddedEvent will
> always see the object already as part of the object hierarchy (which is,
> after all, what ObjectAddedEvent is about).
> 

I found myself doing this quite often.
I use a decorator to make the code read nicer..


class MyContainer(Container):

    @callafteradd
    def setup(self)
        self['subobject'] = SomeObject()
        self['subobject2'] = AnotherObject()
        ...


def callafteradd(method):
    """Decorator which invokes a method after an instance
    has been added to a container.
    """
    caller_locals = sys._getframe(1).f_locals
    if caller_locals.get('__callafteradd__') is None:
        caller_locals['__callafteradd__'] = ()
    caller_locals['__callafteradd__'] += (method,)
    return method


@component.adapter(IObjectAddedEvent)
def ObjectAddedHandler(event):
    callafteradd_methods = getattr(ob, '__callafteradd__', ())
    for f in callafteradd_methods:
        f(ob)

_______________________________________________
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users

Reply via email to