How do you deal with multiple Python classes in the same module depending on each other?
I'm using an ODM library and I'm defining documents as classes within the same module, when they are related. I've hit a circular dependency problem and because I haven't come across this before in Python, I don't know how to inform the classes of the existence of each other. Example:
''' docs.py ''' from mongoengine import Document from mongoengine.fields import StringField, ReferenceField, ListField class Base(Document): some_field = StringField() class Foo(Base): other_field = StringField() another_field = ReferenceField(Bar) class Bar(Base): other_field = StringField() another_field = ListField(ReferenceField(Foo))
As it stands the Python will throw a NameError because Bar is not defined when the interpreter gets to a reference to it in the file, within the class Foo. How do I tell Python not to worry and that the class definition will be along shortly?
ReferenceField accepts class name as well.
another_field = ReferenceField('Bar')