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

Kale Kundert kale at thekunderts.net
Wed Aug 26 19:15:45 CEST 2015


> On Wed, Aug 26, 2015 at 8:11 AM, Sven R. Kunze <srkunze at mail.de> wrote:
> > The issue (not a huge problem again but annoying from time to time) is that
> > the order of declaration in a module actually matters. IIRC other modern
> > languages like C# don't require you do actually care about this anymore.
> >
> > Possible example (for whatever reason an author wants to do that -- also cf.
> > stackoverflow):
> >
> > class UseThis(Base):
> >     pass
> >
> > class UseThat(Base):
> >     pass
> >
> > class Base:
> >     pass
> >
> > In that regard, Python feels a bit rusty.
>
> Frankly, I don't have a problem with this. You get a mandate that
> requires you to do what's good practice anyway: lay things out in a
> logical order. In the same way that Python's use of indentation for
> block structure is generally just enforcing what you'd have done
> regardless of language, this requires that you sort things in
> dependency order. That tends to mean that the first use of any name in
> a module is its definition/source. Want to know what 'frobnosticate'
> means? Go to the top of the file, search for it.
>
> Having that enforced by the language is a restriction, but how often
> does good code have to be seriously warped to fit into that model? Not
> often, in my experience.
>
> ChrisA

Just to provide a concrete example, sqlalchemy's ORM seems to really contort
itself (at least from the user's perspective) to get around this problem.  The
reason in that case is that the dependencies between tables don't have to be
directed acyclic graphs, e.g. it's common for two tables to depend on each other.

I've also run into this problem when working with my home-grown message-passing
APIs, which can also form more complicated dependency graphs.  So I do think
that good code occasionally has to warp itself to fit into python's model.

-Kale


More information about the Python-ideas mailing list