Python access parent object instances
Question: I'm currently trying to write a multiple-file Python (2.6.5) game using
PyGame. The problem is that one of the files, "pyconsole.py", needs to be able
to call methods on instances of other objects imported by the primary file,
"main.py". The problem is that I have a list in the main file to hold
instances of all of the game objects (player's ship, enemy ships, stations,
etc.), yet I can't seem to be able to call methods from that list within
"pyconsole.py" despite the fact that I'm doing a `from pyconsole import *` in
"main.py" before the main loop starts. Is this simply not possible, and should
I instead use M4 to combine every file into 1 single file and then bytecode-
compile and test/distribute that?

Example:

    
    
    bash$ cat test.py
    #!/usr/bin/python
    
    import math, distancefrom00
    foo = 5
    
    class BarClass:
        def __init__(self):
            self.baz = 10
        def get(self):
            print "The BAZ is ", self.baz
        def switch(self)
            self.baz = 15
            self.get()
    
    bar = BarClass()
    
    def main():
        bar.switch()
        print distancefrom00.calculate([2, 4])
    
    if __name__ == '__main__': main()
    
    bash$ cat distancefrom00.py
    #!/usr/bin/python
    
    import math
    import test
    
    def calculate(otherpoint):
        return str(math.hypot(otherpoint[0], otherpoint[1]))+" (foo = "+str(test.foo)+"; "+test.bar.get()+")"
    
    bash$ python test.py
    The BAZ is  15
    The BAZ is  10
    Traceback (most recent call last):
      File "test.py", line 24, in <module>
        if __name__ == '__main__': main()
      File "test.py", line 22, in main
        print distancefrom00.calculate([2, 4])
      File "/home/archie/Development/Python/Import Test/distancefrom00.py", line 8, in calculate
        return str(math.hypot(otherpoint[0], otherpoint[1]))+" (foo = "+str(test.foo)+"; "+test.bar.get()+")"
    TypeError: cannot concatenate 'str' and 'NoneType' objects
    

If my somewhat limited understanding of Python names, classes, and all that
stuff is correct here, the NoneType means that the name `test.bar.get()` \-
and thus, `test.bar` \- is not assigned to anything.


Answer: > The problem is that one of the files, "pyconsole.py", needs to be able to
> call methods on instances of other objects imported by the primary file,
> "main.py".

This just sounds like the dependencies are wrong. Generally nothing should be
calling 'backwards' up to the main file. That main.py should be the glue that
holds everything else together, and nothing should depend on it. Technically
the dependencies should form a [directed acyclic
graph](http://en.wikipedia.org/wiki/Directed_acyclic_graph). As soon as you
find a cycle in your dependency graph, move out the common aspects into a new
file to break the cycle.

So, move the things in 'main.py' that are used by 'pyconsole.py' out into a
new file. Then have 'main.py' and 'pyconsole.py' import that new file.

