4

あるファイルに親クラスがあり、別のファイルに子クラスがあり、それらを 3 番目のファイルで使用しようとしています。このような並べ替え:

test1.py

class Parent(object):
    def spam(self):
        print "something"

test2.py

class Child(Parent):
    def eggs(self):
        print "something else"

test3.py

from test1 import *
from test2 import *
test = Child()

test3.py を実行すると、次のようになります。

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

親クラスと子クラスをすべて同じ場所に保持する必要がありますか?

4

1 に答える 1

6

Parentモデルもインポートする必要がありtest2.pyます

from test1 import Parent

class Child(Parent):
    def eggs(self):
        print "something else"
于 2013-06-05T19:09:27.610 に答える