too simple a question : forward declaration?

Fredrik Lundh fredrik at pythonware.com
Wed May 14 06:33:29 EDT 2003


Helmut Jarausch wrote:

> > You cannot.  Python has no declarations, just
> > executable statements.  Having no declarations,
> > it has, in particular, no FORWARD declarations.
>
> This argument is not a forcing one.

it's not an argument, it's the way it works.

"def" is just another assignment statement (it assigns a
callable function body to a variable).

> Yes, I thought about that, but in my C++ courses I always
> mention the example of two (recursive) functions calling
> each other.

consider this:

    # <-- at this point, neither "a" nor "b" are defined
    # if you attempt to call "a", Python won't find it.

    def a():
        b()

    # <-- at this point, "a" is defined, but "b" is not
    # if you attempt to call "a", it won't find b

    def b():
        a()

    # <-- at this point, both "a" and "b" are defined

    a() # runs, but runs out of stack after a while

> So, how is this impossible in Python ?

not at all.

> Are there cyclically dependent import statements allowed?

as long as you stick to "import" and don't use "from import" unless
you know exactly what you're doing [1], it usually just works.

</F>

1) http://effbot.org/zone/import-confusion.htm








More information about the Python-list mailing list