[Python-ideas] Forward-References & Out-of-order declaration

Nick Coghlan ncoghlan at gmail.com
Sat Aug 29 06:41:04 CEST 2015


On 27 August 2015 at 09:41, Chris Angelico <rosuav at gmail.com> wrote:
> The two basic solutions still apply: either use string names to
> identify not-yet-defined objects, or have a "pre-declare" syntax to
> make things possible. In C, "struct foo;" is enough to let you declare
> pointers to foo; in Python, you could have "Thing = Table()" prior to
> defining Manufacturer, and then you could use an unquoted Thing to
> define the relationship. Either way makes it clear that something
> unusual is happening.

It's also the case that *circular dependencies hint at a design
problem*. They're sometimes an unavoidable problem (because you're
modelling a genuinely bidirectional relationship), but they're still a
problem, since acyclic models structurally avoid a *lot* of the
challenges that come up when cycles may be present (for example,
consider how much easier it is to traverse a filesystem tree if you
*don't* support following symlinks).

Teasing apart a data model (which is what a class hierarchy
represents) to either eliminate the circular references, or else limit
them to within particular files is actually a pretty good way to
figure out which parts of that model are tightly coupled, and which
are more loosely related.

Regards,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list