Returning another instance from constructor

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


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

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531

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

Regards,
Dan

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>
Never play leapfrog with a unicorn.



More information about the Python-list mailing list