Returning another instance from constructor

Robert Brewer fumanchu at
Sat Jul 31 04:53:04 CEST 2004

Edward Diener wrote:
> Is there a way in Python to have the constructor of a class 
> "return" another
> instance of the same class ? I am well aware of the fact that 
> __init__ does
> not return anything, but I would love to do something like this:
> class X(object):
>     def __init__(self,...other parameters):
>         # some magic code
> x = X()
> y = X()
> and have y actually referencing x automatically.

Sounds like you want a Singleton. My first thought was to override
__new__; that approach (among many others) is covered pretty well at -- JeremyBowers shows how to do
it with __new__.


More information about the Python-list mailing list