(Oops, forgot to change this to go to the list...) On 4/14/05, Orri Ganel <[EMAIL PROTECTED]> wrote: > On 4/14/05, Rich Krauter <[EMAIL PROTECTED]> wrote: > > On 4/14/05, Max Noel <[EMAIL PROTECTED]> wrote: > > Well, if you want b and a to refer to the same object, just use b = > > a. > > If you'll look at my code, you'll see that I *did* try that approach, > and it did not persist past __init__ for some reason:
That's because you re-bound the local name 'self', but that doesn't affect anything outside the local namespace. Remember, __init__() returns None; the object has already been allocated before __init__() is called, and while __init__() can mutate the object it can't affect the reference that the interpreter has created. (Unless you resort to hacking the contents of the parent frame, but we won't get into that. You *don't* want to do this.) In this case, I agree that a factory is your best bet. Jeff Shannon > > class Node: > ... > def __init__(self, cargo=None, prev=None, next=None, nod=False): > """x.__init__(...) initializes x; see > x.__class__.__doc__ for signature""" > if not isinstance(cargo, Node) or nod: > self.cargo = cargo > self.prev = prev > self.next = next > else: > ################################# > self = cargo ## see? > ################################# > print id(self), id(cargo) > print self.cargo > > >>> a = Node(1) > >>> b = Node(a) > 12932600 12932600 > 1 > >>> id(b) > 12960632 > > -- > Email: singingxduck AT gmail DOT com > AIM: singingxduck > Programming Python for the fun of it. > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor