Are circular dependencies possible in Python?

Dave Brueck dave at pythonapocrypha.com
Sat Apr 9 12:23:08 EDT 2005


Tim Tyler 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.

Yes and no. Yes, they have to exist before you can use them (that only makes 
sense), but no, they don't have to be placed in the same order in the source 
code like they do with C:

 >>> def A(count):
...   print 'A', count
...   B(count + 1)
...
 >>> def B(count):
...   print 'B', count
...   if count < 10:
...     A(count + 1)
...
 >>> B(0)
B 0
A 1
B 2
...

See - all that matters is that they exist before you call them.

-Dave



More information about the Python-list mailing list