Basic 'import' problem

Mel Wilson mwilson at the-wire.com
Sat Feb 7 22:44:56 CET 2004


In article <c03kmp$28u7$1 at ns.felk.cvut.cz>,
Frantisek Fuka <fuka at fuxoft.cz> wrote:
>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".

   To evade your problem, design your modules (as much as
you can) as though they were subroutine libraries usable
from any program.

   If you can't do that, maybe pass the main-level functions
as parameters to the lower-level module functions, as
"callbacks".

   A really filthy, but workable, last resort would be for
main-level code to poke the necessary function into the
sub-modules namespace, e.g.

        ==================================
        "sub1.py"
        sub1_func1 ():
            main_func1()


        ==================================
        "base.py"
        import sub1
        def func1 ():
            pass
        sub1.main_func = func1


   Another thought, in sub1.py, define a class with most of
the functionality.  Then, in base.py, define a descendant of
that class with its own special method to perform the
main-level operation.



        Good Luck.      Mel.



More information about the Python-list mailing list