Python:从不同模块导入父类和子类

use*_*571 4 python oop python-import python-2.7

我在一个文件中有一个父类,在另一个文件中有一个子类,我试图在第三个文件中使用它们.有点像:

test1.py

class Parent(object):
    def spam(self):
        print "something"
Run Code Online (Sandbox Code Playgroud)

test2.py

class Child(Parent):
    def eggs(self):
        print "something else"
Run Code Online (Sandbox Code Playgroud)

test3.py

from test1 import *
from test2 import *
test = Child()
Run Code Online (Sandbox Code Playgroud)

运行test3.py给我以下内容:

File "[path]\test2.py", line 1, in <module>
class Child(Parent):
NameError: name 'Parent' is not defined
Run Code Online (Sandbox Code Playgroud)

我是否需要将我的父类和子类保持在同一个地方?

kar*_*ikr 6

您需要导入Parent模型test2.py,以及

from test1 import Parent

class Child(Parent):
    def eggs(self):
        print "something else"
Run Code Online (Sandbox Code Playgroud)