[Twisted-Python] Consistent interfaces to asynchronous partially-available services using Deferreds and state machines (was Re: Another approach to allowing __init__ to work with Deferreds)
Tristan Seligmann
mithrandi at mithrandi.net
Wed May 20 16:10:30 MDT 2009
On Tue, May 12, 2009 at 1:39 PM, Terry Jones <terry at jon.es> wrote:
> If Python allowed me to return a deferred from __init__, my problem would
> vanish. That's not going to happen though, I know :-)
Well, if you /really/ want to, you can implement that; something like:
####
from twisted.internet import defer
class DeferredClass(object):
def __new__(cls, *a, **kw):
inst = super(DeferredClass, cls).__new__(cls, *a, **kw)
return defer.maybeDeferred(inst.__init__, *a,
**kw).addCallback(lambda ign: inst)
####
Example usage:
####
class Example(DeferredClass):
def __init__(self, value):
def _cb(value):
self.value = value
return defer.succeed(value).addCallback(_cb)
def _printIt(x):
print x.value
Example(42).addCallback(_printIt) # Prints "42"
####
Of course, this doesn't implement your magic wrapping behaviour, so
maybe that's not what you meant.
--
mithrandi, i Ainil en-Balandor, a faer Ambar
More information about the Twisted-Python
mailing list