[Tutor] odd behavior within __init__

Orri Ganel singingxduck at gmail.com
Fri Apr 15 05:02:32 CEST 2005


On 4/14/05, Rich Krauter <rmkrauter at yahoo.com> wrote:
> Maybe you could use a factory. It would allow you to simplify your Node
> class, and encapsulate the instantiation behavior you want outside the
> class.

Thanks for the suggestion; I think that's what I'll do. 

On 4/14/05, Max Noel <maxnoel_fr at yahoo.fr> 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:

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.


More information about the Tutor mailing list