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

Cloudant number of database limitation

couchdb cloudant

I'm planing on having my database stored in Cloudant.

Sphinx - How to escape user input for SphinxQL?

php sphinx sphinxql

I have website where users can search posts by entering keywords,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.