Python: importing parent and child classes from different modules

I have a parent class in one file, a child class in another, and I'm trying to use them in a third. Example:

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() 

running test3.py gives me the following:

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

Do you just need to keep the parent and child classes in one place?

+4
source share
1 answer

You need to import the Parent model into test2.py as well

 from test1 import Parent class Child(Parent): def eggs(self): print "something else" 
+6
source

Source: https://habr.com/ru/post/1484707/


All Articles