OO issues in python
Terry Reedy
tjreedy at udel.edu
Wed Dec 17 22:19:01 EST 2003
More information about the Python-list mailing list
Wed Dec 17 22:19:01 EST 2003
- Previous message (by thread): OO issues in python
- Next message (by thread): OO issues in python
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Ali El Dada" <eldada at mdstud.chalmers.se> wrote in message news:brpar5$c47$1 at eol.dd.chalmers.se... > in python, when a class, say Father, has a member that itself is an > instance of another class, say Child, an instance of Father actually > only has a reference to a Child, not the Child object itself, right? Good question. As you defined the difference by quoting from Eiffel, yes. The association relating names and collection slots to objects is many to one. A given object can be associated with (referenced by) multiple names, lists, dicts, and instances. This is essential to Python's operation. If you want to restrict the association to be one to one (as in engine belongs to only one car), you must exert the discipline yourself. You can facilitate this by making the restricted class private to a module or another class. I have never heard of this being a problem. > eg code that doesn't work: > class Father: def __init__(self): self.a = 'foo' self.son = Child() class Child: def __init__(self): print f.a f = Father() Put on your Python interpreter hat and try to execute this. Father calls Father.__init__ calls Child calls Child.__init__ tries to access object named f, but there is no such object because the calls have not returned and hence the binding of global f to Father instance has not yet occurred and hence you get a NameError. >>> f = Father() Traceback (most recent call last): File "<stdin>", line 1, in ? File "<stdin>", line 4, in __init__ File "<stdin>", line 3, in __init__ NameError: global name 'f' is not defined (Please, when you post code that 'does not work', cut and paste the error traceback as above.) If you want to create a circular reference between Father and Child, pass the Father instance to the Child initialization. class Father: def __init__(self): self.a = 'foo' self.son = Child(self) class Child: def __init__(self, father): self.father = father print father.a >>> f = Father() foo Terry J. Reedy
- Previous message (by thread): OO issues in python
- Next message (by thread): OO issues in python
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list