Re: [Twisted-Python] A Python metaclass for Twisted allowing __init__ to return a Deferred
![](https://secure.gravatar.com/avatar/c8f4d4d1a8fc53c13ed05c202e0257fe.jpg?s=120&d=mm&r=g)
"Esteve" == Esteve Fernandez <esteve@sindominio.net> writes: Esteve> I'm going to jump in. How about this:
That's nice. But you've coupled the args to aFuncReturningADeferred and what it's Deferred ends up returning to the args for __init__ of the class. I think you meant this: from twisted.internet import defer, reactor def aFuncReturningADeferred(value): d = defer.Deferred() reactor.callLater(5, d.callback, value[::-1]) return d class Foo(object): def __new__(cls, *args, **kw): def cb(x): obj = object.__new__(cls) obj.__init__(*args, **kw) return obj return aFuncReturningADeferred("Some value").addCallback(cb) def __init__(self, value): self.value = value def printFoo(obj): print obj.value reactor.stop() d = Foo("Some other value") d.addCallback(printFoo) reactor.run() I.e., __new__ can do whatever it likes with preparing args for aFuncReturningADeferred (including using args and kw), and all of the args to __new__ are passed to __init__, as the caller would expect from a normal class. You're passing the return result of the Deferred as the single arg to __init__. Anyway, I like it more than my solution. Terry
![](https://secure.gravatar.com/avatar/a317a8f80c2fc9e5df8470c599e89e2c.jpg?s=120&d=mm&r=g)
On Monday 03 November 2008 14:11:05 Terry Jones wrote:
"Esteve" == Esteve Fernandez <esteve@sindominio.net> writes:
Esteve> I'm going to jump in. How about this:
That's nice. But you've coupled the args to aFuncReturningADeferred and what it's Deferred ends up returning to the args for __init__ of the class.
Yep, that's what I meant. I wanted to show how to pass an argument to Foo, which will be used to call a function (that may take a considerable amount of time) and whose return value will be stored in a Foo instance. Following your example at http://bit.ly/1whZUK, you may pass the database name as an argument to DatabaseClass, call prepareConnection (a function that returns a Deferred) in the __new__ method to create the necessary tables and pass a ConnectionPool in the Deferred chain, so it can end up being stored as an instance variable of a DatabaseClass object. Cheers.
![](https://secure.gravatar.com/avatar/a317a8f80c2fc9e5df8470c599e89e2c.jpg?s=120&d=mm&r=g)
On Monday 03 November 2008 14:53:24 Esteve Fernandez wrote:
On Monday 03 November 2008 14:11:05 Terry Jones wrote:
> "Esteve" == Esteve Fernandez <esteve@sindominio.net> writes:
Esteve> I'm going to jump in. How about this:
That's nice. But you've coupled the args to aFuncReturningADeferred and what it's Deferred ends up returning to the args for __init__ of the class.
Yep, that's what I meant. I wanted to show how to pass an argument to Foo, which will be used to call a function (that may take a considerable amount of time) and whose return value will be stored in a Foo instance.
Alternatively a function would suffice: from twisted.internet import defer, reactor def aFuncReturningADeferred(value): d = defer.Deferred() reactor.callLater(5, d.callback, value[::-1]) return d class SimpleFoo(object): def __init__(self, value): self.value = value def printFoo(obj): print obj.value reactor.stop() def FooFactory(value): return aFuncReturningADeferred(value).addCallback(SimpleFoo) d = FooFactory("Some value") d.addCallback(printFoo) reactor.run()
participants (2)
-
Esteve Fernandez
-
Terry Jones