[Twisted-Python] A Python metaclass for Twisted allowing __init__ to return a Deferred
terry at jon.es
Mon Nov 3 09:20:22 EST 2008
>>>>> "Esteve" == Esteve Fernandez <esteve at sindominio.net> writes:
Esteve> Alternatively a function would suffice:
Esteve> def FooFactory(value):
Esteve> return aFuncReturningADeferred(value).addCallback(SimpleFoo)
Right. That's JP's solution (though he didn't bother wrapping it in a
func): pass the result of the Deferred to the constructor of the class.
But I like your __new__ approach better, as it puts the logic for creating
deferreds, adding callbacks to them, doing other stuff etc., into the class
itself. It's in __new__ instead of __init__, but that seems perfect to me.
My approach, like all of them (and necessarily so) was two-phase, but my
second phase required the calling of __instantiate__. By using __new__ and
__init__ instead of my __init__ and __instantiate__, you moved the phases
back and just hitch-hike on Python's existing machinery. That's why I like
that approach, plus, as I keep saying, it keeps the Deferred logic inside
the class where it belongs.
More information about the Twisted-Python