469,610 Members | 1,649 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

Returning another instance from constructor

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.

Of course I am aware that if X is passed an optional parameter of type X in
its constructor, it can forward all its member functions to that other X.
But I am looking for something even more magical. I thought that maybe I
could do such magic using metaclasses, but metaclasses only create classes
and not class instances, so I do not see how. If anyone has any ideas I
would love to hear it.
Jul 18 '05 #1
2 1357
On Sat, 31 Jul 2004 02:03:49 GMT,
"Edward Diener" <el******@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/Coo...n/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.
Jul 18 '05 #2
Dan Sommers wrote:
On Sat, 31 Jul 2004 02:03:49 GMT,
"Edward Diener" <el******@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/Coo...n/Recipe/66531

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


I am using new style classes and I see:

# here is the new-style Borg (not much more complex then the "old-style")
class Borg(object):
_state = {}
def __new__(cls, *p, **k):
self = object.__new__(cls, *p, **k)
self.__dict__ = cls._state
return self

It seems in my case above I need to say for y, when I am able to recognize
that it should actually be the same as x:

self.__dict__ = x.__dict__

to have y be the same as x. Is this correct ? Also I can find no
documentation in the Python 2.3 docs for __new__ . Can you point me to it ?
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Patrick | last post: by
8 posts views Thread by briforge | last post: by
2 posts views Thread by TEK | last post: by
11 posts views Thread by Paulo da Silva | last post: by
3 posts views Thread by Peter Oliphant | last post: by
7 posts views Thread by Thomas Lenz | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.