Returning another instance from constructor

Dan Sommers me at
Sat Jul 31 22:47:28 CEST 2004

On Sat, 31 Jul 2004 02:03:49 GMT,
"Edward Diener" <eldiener at> 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.

The Borg pattern may be useful:

x and y will still be their own objects, but they'll share all of their
state information.


Dan Sommers
Never play leapfrog with a unicorn.

More information about the Python-list mailing list