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?

Answers


ReferenceField accepts class name as well.

another_field   = ReferenceField('Bar')

Need Your Help

iOS GLSL Spherical Harmonic Shader. How Do I Raycast Using the iOS GPU

ios opengl-es glsl raycasting pixel-shading

I am beginning an Spherical Harmonics shader project for an iOS app I am writing. I have begun by reading this excellent in-depth paper on the subject (PDF) - http://bit.ly/aQmax3.

Azure - Extra small instance web role - ready for production?

azure load-balancing azure-web-roles webrole

I'm planning for a website running in Azure. I'm estimating max. 2000 users a day creating about 20.000 hits.