Circular module dependency in python
Question: I have two modules, baselib.Database and baselib.Application. In
baselib.Application, I have

    
    
    import baselib.Database
    
    APP = None
    class BaseApplication():
        def __init__(dbClass = baselib.Database.GenericDb...):
            global APP
            this.dbClass = dbClass
            APP = this
            etc...
    

In baselib.Database, I have

    
    
    import baselib.Application
    
    def getDB(dbClass = baselib.Application.APP.dbClass):
        etc...
    
    class GenericDB():
        def __init__(self, args):
        etc...
    

The problem is, when I import either of these modules, I get

    
    
    AttributeError: 'module' object has no attribute (modulename)
    

The problem seems to stem from the fact that the default arguments are
evaluated during the import; if I replace getDB with

    
    
    def getDB(dbClass = None):
        dbClass = dbClass or baselib.Application.APP.dbClass
    

and do the same thing with the other default argument, everything works fine.
Is this the best/most pythonic way to do this, or should I just avoid the
circular dependency entirely and combine the two modules into one file? I'd
really like to keep them separate because a large part of my codebase is
dependent on them.


Answer: I think this <http://effbot.org/zone/import-confusion.htm#circular-imports>
may answer your question. Basically, import is also a statement. We should
avoid circular module dependency.

