Is this type of forward referencing possible?

Miles semanticist at gmail.com
Sun Mar 15 17:22:00 CET 2009


On Sun, Mar 15, 2009 at 11:33 AM, Paul McGuire wrote:
> I'm guessing the class mapping is all part of an overall design, so I
> would define all of these after creating A and B as empty classes:
>
> class A: pass
> class B: pass
>
> A.someType = B
> B.anotherType = A

While this would work in the general case, it's more iffy with ORM
tools like the App Engine Datastore API.  Those classes often have
metaclasses that process the properties when the classes are created,
and tacking on additional properties later is unlikely to work (I
suspect the OP neglected to point out that A and B likely both inherit
from db.Model).

The Django solution is to allow forward references in the form of
strings of class names (e.g., ReferenceType('B')).  It doesn't look
like App Engine has a solution for this situation:
http://code.google.com/p/googleappengine/issues/detail?id=811

-Miles



More information about the Python-list mailing list