Simple cross import in python

老子叫甜甜 提交于 2019-11-27 23:34:08

Instead of importing the modules on top, you could import the other module within the hello function.

class B():
    def __init__(self):
        print "B"

    def hello(self):
        import lib.A
        print "hello B"
        a = A()

Your main problem is that you're trying to import a class, but using syntax that only works to import a module. Specifically, import lib.A is never going to work if A is a class defined in module lib.a (and imported into the top-level namespace of lib).

What I suggest is that you avoid using the from _ import _ syntax unless you really need it. That makes the dependencies much easier to resolve:

lib/a.py:

import lib.b # note, we're not importing the class B, just the module b!

class A():
    def foo(self):
        return lib.b.B() # use the class later, with a qualified name

lib/b.py:

import lib.a # again, just import the module, not the class

class B():
    def foo(self):
        return lib.a.A() # use another qualified name reference

lib/__init__.py:

from a import A # these imports are fine, since the sub-modules don't rely on them
from b import B # they can be the public API for the A and B classes

You could also use relative module imports if you don't want a and b to be dependent on the name of their package lib.

This is sure to work because neither class A or B actually requires the other to exist yet in order to be defined. It's only after they're imported that instances of A need to know about the class B (and vise versa).

If one of the classes inherited from the other, or otherwise used an instance of the other at top level, you'd need to be more careful about which module was loaded up first, or it might still break.

When you have two classes depending on each other usually means that either they really belong to the same module or that you have a too tight coupling that should be resolved using dependency injection.

Now there are indeed a couple corner cases where importing from within the function is the "least worst" solution but that's still something you should avoid as much as possible.

If you want to import it only once you can import it in the constructor of the class and make the variable global:

class B():
    def __init__(self):
        global A
        from lib import A
        print "B"

    def hello(self):
        print "hello B"
        a = A()

This would import A into a global variable and make it accessible form within the module.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!