How did python handle forward declaration?

Ben Hutchings ben.hutchings at
Tue Feb 20 23:16:15 CET 2001

javalist <javalist at> writes:

> Hello python-list,
>         in c/c++ we can use class xxx or struct xxx to forward
>         declared what we really implement later,how python work this
>         out?

C and C++ compilers try to resolve all identifiers and type names at
the point they are used, at compile time.  This is a problem if you
need to define two classes/structures that refer to each other.  For
some purposes they can accept a declaration that promises there will
be a definition somewhere else (later, or in another translation
unit), and that's how you deal with that need.

Python is a much more dynamic language, which postpones almost all
name resolution to run-time.  The external things (classes, modules,
etc) that a piece of code relies on just need to be defined at the
time it gets to run.  When you define a class, none of the code in its
member functions runs, so you can define two classes/structures that
refer to each other without any need for forward-declarations.

You might have problems in Python if you want to define mutually-
dependent modules, but then a mutually-dependent set of modules
is effectively just one module and should be defined as such.

Any opinions expressed are my own and not necessarily those of Roundpoint.

More information about the Python-list mailing list