cause __init__ to return a different class?

Chris Rebert clp2 at rebertia.com
Thu Sep 15 07:35:26 CEST 2011


On Wed, Sep 14, 2011 at 10:20 PM, Matthew Pounsett
<matt.pounsett at gmail.com> wrote:
> I'm wondering if there's a way in python to cause __init__ to return a class other than the one initially specified.  My use case is that I'd like to have a superclass that's capable of generating an instance of a random subclass.
<snip>
> Is there a way to do this?

Override __new__() instead:
http://docs.python.org/reference/datamodel.html#object.__new__

Cheers,
Chris



More information about the Python-list mailing list