Returning another instance from constructor

Discussion in 'Python' started by Edward Diener, Jul 31, 2004.

  1. 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.
    Edward Diener, Jul 31, 2004
    #1
    1. Advertising

  2. Edward Diener

    Dan Sommers Guest

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


    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.
    Dan Sommers, Jul 31, 2004
    #2
    1. Advertising

  3. Dan Sommers wrote:
    > On Sat, 31 Jul 2004 02:03:49 GMT,
    > "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.

    >
    > 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.


    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 ?
    Edward Diener, Aug 1, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Graham
    Replies:
    3
    Views:
    513
    Matt Graham
    Feb 7, 2004
  2. Asfand Yar Qazi
    Replies:
    6
    Views:
    15,627
    jeffc
    May 17, 2004
  3. Neroku
    Replies:
    8
    Views:
    1,197
    Tor Iver Wilhelmsen
    Nov 18, 2006
  4. Andy
    Replies:
    9
    Views:
    507
    Pete Becker
    Sep 24, 2006
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,221
Loading...

Share This Page