Ok I have two modules, each containing a class, the problem is their classes reference each other.
Lets say for example I had a room module and a person module containin
Do you actually need to reference the classes at class definition time? ie.
class CRoom(object):
person = CPerson("a person")
Or (more likely), do you just need to use CPerson in the methods of your class (and vice versa). eg:
class CRoom(object):
def getPerson(self): return CPerson("someone")
If the second, there's no problem - as by the time the method gets called rather than defined, the module will be imported. Your sole problem is how to refer to it. Likely you're doing something like:
from CRoom import CPerson # or even import *
With circularly referencing modules, you can't do this, as at the point one module imports another, the original modules body won't have finished executing, so the namespace will be incomplete. Instead, use qualified references. ie:
#croom.py
import cperson
class CRoom(object):
def getPerson(self): return cperson.CPerson("someone")
Here, python doesn't need to lookup the attribute on the namespace until the method actually gets called, by which time both modules should have completed their initialisation.