Basic 'import' problem

Diez B. Roggisch deets_noospaam at web.de
Sat Feb 7 15:33:50 EST 2004


> I think I grasp why I got this error (file2.hello2() is referenced
> before its definition was "compiled" by Python) but I am puzzled as to
> how to do this properly. Isn't it fairly common for two source files to
> reference each other in this way? I know I can solve this by putting the
> import statement after the def statement, but I have the similar problem
> in much larger project, with many files and many includes and I'd like
> to keep all my includes at the beginning of the file.

First of all, don't use the name "file" - your are shadowing the type file
with it. 

Now to your problem: Its perfectly legal to have circular dependencies at
declaration level - thus this works in python:

def foo():
   bar()

def bar():
   if moon_is_in_the_third_house:
       foo()

In C, you had to define a prototype of bar, so that foo can use it.

But what you try is to already execute code from file in file2 _while_
importing file2 inside file, because you have statements on the
module-level - and that can't work. And I doubt that a language exists
where thats possible.

-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list