Basic 'import' problem

Frantisek Fuka fuka at fuxoft.cz
Sat Feb 7 22:18:50 CET 2004


Diez B. Roggisch wrote:
>>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. 

OK. I used that nome only in this simplified exmaple.

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

Yes, this seems logical. It looks like I have some fundamental problems 
with grasping the philosophy of Python...

My application is started from base.py. This file imports many other 
files (modules) that do different stuff. Base.py references dozens of 
classes and functions in other modules. After a while, there comes a 
time when some of these modules need to reference some basic function 
inluded in base.py. Which means (I thought until now) that I have to 
include base.py in these files. Or not? Are you saying there is no 
chance of doing this? Let's forget "import" for now and please explain 
to me: Is there way to create a Python application consisting of several 
modules that can freely call and reference stuff in each other? From 
your answer it seems that the anwer is "no".

-- 
                                          Frantisek Fuka
(yes, that IS my real name)
(and it's pronounced "Fran-tjee-shek Foo-kah")
----------------------------------------------------
My E-mail:   fuka at fuxoft.cz
My Homepage: http://www.fuxoft.cz
My ICQ:      2745855



More information about the Python-list mailing list