too simple a question : forward declaration?

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed May 14 06:10:16 EDT 2003


"Helmut Jarausch" <jarausch at igpm.rwth-aachen.de> wrote in 
news:3EC210E3.7000208 at igpm.rwth-aachen.de:

> Yes, I thought about that, but in my C++ courses I always
> mention the example of two (recursive) functions calling
> each other.
> So, how is this impossible in Python ?
This is not a problem so long as both functions are defined at the point 
where one of them is first called.

e.g.

def fn1():
   ...
   fn2()

def fn2():
   ...
   fn1()

fn1()

The first call that is executed occurs after both functions have been 
defined so there is no problem.

> Are there cyclically dependent import statements
> allowed?

Yes. When a module is imported Python looks in sys.modules to see if it can 
find it. It there is an entry in sys.modules it simply returns it. 
Otherwise it looks to see if there is something it can import (e.g. a .py 
file), and if there is, it creates a new entry in sys.modules and then 
executes the module.

If anything else attempts to import the same module before the execution of 
the module has completed, the import will find the new module in 
sys.modules and simply return it. This module may not have all the names 
available that will be present when execution of the module has completed, 
but so long as you don't attempt to access a value that hasn't yet been 
defined cyclic module imports are perfectly reasonable.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list