Basic 'import' problem
Diez B. Roggisch
deets_noospaam at web.de
Sun Feb 8 11:58:42 EST 2004
> 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".
Not really. I try to explain the difference without the import-stuff as you
suggested:
What works is this:
def a():
# calls a function that is defined later
b()
def b():
# calls a()
a()
Now this example would be endlessly recursive, but thats not the point right
now.
now what you try to do is this:
c()
def c():
...
Please note the difference here: The _call_ is made on the indentation-level
0, thus c() gets called _before_ it is defined! Somebody in this thread
said such things were possible with PL/1 - well, that might be, but only if
you had a two-pass-compilation scheme that ignores statements in the first
pass. But your _module_ is _executing_code_ that whilst it is beeing
imported. Thats not working. And I don't see what good thats for - of
course you can do initalization-stuff then, but this must rely _only_ on
the module itself, not something thats defined later. If your app is
created in such a way that things are executed in such a difficult way,
that clearly shows bad design - it would e.g. depend on the order of things
beeing imported, and that shouldn't make a difference. You will create a
nightmare for debugging then.
If you have code that is used by several modules, factorize it out and
import it on all occasions its used.
--
Regards,
Diez B. Roggisch
More information about the Python-list
mailing list