Are circular dependencies possible in Python?

Dan Sommers me at privacy.net
Sat Apr 9 12:39:48 EDT 2005


On Sat, 9 Apr 2005 15:57:15 GMT,
Tim Tyler <tim at tt1lock.org>  wrote:

> Like C, Python seems to insist I declare functions before calling them
> - rather than, say, scanning to the end of the current script when it
> can't immediately find what function I'm referring to.

Python has no such restriction.

The only restriction is that the function be defined before it is
called.  Unlike C, function definitions in Python are executable
statements.

If your script looks like this:

    f( ) # call f

    def f( ) # define f
      print 'hello'

then it will fail because f has not yet been defined before it was
called.

> Does Python allow you to do something similar?

No.  Python has no such thing as a function declaration.

> If not how do you create functions with circular dependencies in
> Python - where function A could call function B; and function
> B could call function A - or is that not possible?

Perhaps if you can post a minimal example that shows us what you're
running into, someone here will know how to help.

HTH,
Dan

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>
μ₀ × ε₀ × c² = 1



More information about the Python-list mailing list