Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的简单交叉导入_Python_Import_Cross Reference - Fatal编程技术网

python中的简单交叉导入

python中的简单交叉导入,python,import,cross-reference,Python,Import,Cross Reference,我想在不同的类中分离代码,并将它们放在不同的文件中。然而,这些类是相互依赖的 main.py: from lib import A, B def main(): a = A() b = B() a.hello() b.hello() if __name__ == '__main__': main() import lib.B class A(): def __init__(self): print "A" def

我想在不同的类中分离代码,并将它们放在不同的文件中。然而,这些类是相互依赖的

main.py:

from lib import A, B

def main():
    a = A()
    b = B()
    a.hello()
    b.hello()

if __name__ == '__main__':
    main()
import lib.B

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

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

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

    def hello(self):
        print "hello B"
        a = A()
lib/_init.py

from a import A
from b import B
lib/a.py:

from lib import A, B

def main():
    a = A()
    b = B()
    a.hello()
    b.hello()

if __name__ == '__main__':
    main()
import lib.B

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

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

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

    def hello(self):
        print "hello B"
        a = A()
lib/b.py:

from lib import A, B

def main():
    a = A()
    b = B()
    a.hello()
    b.hello()

if __name__ == '__main__':
    main()
import lib.B

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

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

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

    def hello(self):
        print "hello B"
        a = A()
在Python中可以这样做吗?

编辑:

from lib import A, B

def main():
    a = A()
    b = B()
    a.hello()
    b.hello()

if __name__ == '__main__':
    main()
import lib.B

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

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

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

    def hello(self):
        print "hello B"
        a = A()
我收到以下错误消息:

pydev debugger: starting
Traceback (most recent call last):
  File "eclipse-python/plugins/org.python.pydev_2.7.1.2012100913/pysrc/pydevd.py", line 1397, in <module>
    debugger.run(setup['file'], None, None)
  File "eclipse-python/plugins/org.python.pydev_2.7.1.2012100913/pysrc/pydevd.py", line 1090, in run
    pydev_imports.execfile(file, globals, locals) #execute the script
  File "main.py", line 2, in <module>
    from lib import A, B
  File "lib/__init__.py", line 1, in <module>
    from a import A
  File "lib/a.py", line 1, in <module>
    import lib.B
ImportError: No module named B
pydev调试器:正在启动
回溯(最近一次呼叫最后一次):
文件“eclipsepython/plugins/org.python.pydev_2.7.1.2012100913/pysrc/pydevd.py”,第1397行,在
运行(安装程序['file'],无,无)
文件“eclipsepython/plugins/org.python.pydev_2.7.1.2012100913/pysrc/pydevd.py”,第1090行,正在运行
pydev_imports.execfile(文件、全局、局部)#执行脚本
文件“main.py”,第2行,在
从库导入A、B
文件“lib/\uuuu init\uuuuu.py”,第1行,在
从进口
文件“lib/a.py”,第1行,在
导入lib.B
ImportError:没有名为B的模块

您可以在hello函数中导入另一个模块,而不是导入顶部的模块

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

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

如果只想导入一次,可以在类的构造函数中导入,并使变量为全局变量:

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

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

这将把一个全局变量导入到一个全局变量中,并使其可以在模块内访问。

当您有两个相互依赖的类时,通常意味着它们实际上属于同一个模块,或者您的耦合太紧,应该使用依赖项注入来解决


现在确实有两种情况,从函数内部导入是“最差”的解决方案,但这仍然是您应该尽量避免的。

您的主要问题是,您试图导入一个类,但使用的语法只能导入一个模块。特别是,如果
A
是在模块
lib.A
中定义的类(并导入到
lib
的顶级命名空间中),那么
import lib.A
将永远无法工作

我建议您避免使用
from\uimport
语法,除非您确实需要它。这使得依赖关系更容易解决:

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
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
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
lib/b.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
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
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
lib/\uuuuu init\uuuuu.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
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
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
如果不希望
a
b
依赖于其包
lib
的名称,也可以使用相对模块导入

这是肯定会起作用的,因为A类或B类实际上都不需要存在另一类才能定义。只有在它们被导入之后,A的实例才需要了解B类(反之亦然)


如果其中一个类继承自另一个类,或者以其他方式在顶层使用另一个类的实例,那么您需要更加小心先加载的是哪个模块,否则它可能仍然会损坏。

您尝试过吗?出了什么问题?可以对整个类进行这样的导入,或者我在需要lib.A的每个方法中都这样做?您可以在顶部使用
import lib
,然后使用
lib.A()
如果键入太多,请尝试
import lib as x
并使用
x.A()
更好地使用
from lib import A