Problem with object-in-object and common superclass

ikaran at my-deja.com ikaran at my-deja.com
Mon Sep 25 11:29:37 CEST 2000


Dear all:

I'm trying to write an interface between Python
and Geomview, the 3D object viewer; this is for a
neural net sandbox I'm writing.  I've encountered
the following problem:  An object cannot contain
another object if the classes for both have a
common superclass.  When I try to access data
and/or call a method in the contained object, I
run into an infinite recursion.

Here's a quick program that demonstrates the
problem:

################################

class Container :
  stuff = []
  def show(self) :
    for thing in self.stuff :
      if isinstance(thing, Container) :
        thing.show()
      else :
        print thing

class Pod(Container) :
  def __init__(self) :
    self.stuff.append("pea")

class Plant(Container) :
  def __init__(self) :
    self.stuff.append(Pod())

sprout = Plant()
sprout.show()

################################

The command run ad infinitum is "print 'pea'"; the
problem seems to be that the identifier "stuff"
points to the same Python entity from within both
"Pod" and "Plant."

Questions:

*  Am I making an obvious error in logic with my
program structure that has nothing to do with
Python?  One possibility, I suppose, is that I am
accessing the "stuff" in "Pod" from "Plant," and
hence I am "fooling" the interpreter into
accessing the "Plant" namespace rather than the
"Pod" namespace.  From an execution standpoint, is
"Plant" the innermost scope?

*  If this is a common problem in Python
programming, what is the canonical solution?

*  If my logic is on target, is this scope
behavior a bug, buglet, or design decision?  If
the former, has it been fixed; if the latter, why?

I am running C Python 1.5.2 on Linux 2.2.x.

Regards,

Sourav Mandal
http://www.ikaran.com/Sourav.Mandal/


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list