On May 16, 2015, at 8:39 AM, Ben Darnell <[email protected]> wrote:
> 
> Personally I'd rather use an explicit factory function than __new__ magic 
> here, but it's possible to experiment with this style.
> 

I can tell you from experiments in the Twisted community that this kind of 
"async object creation" without an explicit factory function is basically an 
antipattern, for the following reasons:

it interacts weirdly with inheritance and metaclasses; figuring out whose 
__new__ is going to be called and why always turns into an advanced calculus 
problem
it has surprising behavior with respect to __init__
it tightly couples a specific way of initializing a particular object with the 
residence of that object in memory. any object with an asynchronous initializer 
can't be deserialized normally, for example, because __new__ is now doing 
something surprising
it makes testing harder, because now you can't even create a simple test object 
without a whole event loop running

Fundamentally, object initialization is a local phenomenon.  It's a data 
structure that your program is using to think about the world.  When you 
"asynchronously initialize" something, you are doing two things: the remote 
gathering of information which will initialize the object, and the local 
construction and initialization.  The former phase should be a method (which 
can be a classmethod) and the latter is the object's constructor itself.

More broadly, having constructors do I/O is an anti-pattern for these same 
reasons; you just notice it faster in an async framework :).

-glyph

Reply via email to