I have been searching high and low for an answer and cannot seem to find one. I am running into a fundamental issue when attempting to import a class from another file. I am relatively new to Python and OOP in general, so forgive me if my query is rudimentary.
The Issue: I want to import a CHILD class into a PARENT class. Simple enough, but when I import the class it immediately executes.
The Question: How do I import a class so it can be referenced globally in my parent class?
Here is a basic example of the PARENT class:
from child import CHILD
class PARENT:
def _init_(self):
print "START PARENT CLASS"
def goTo(self,enter):
if enter == "1":
c.childScreen()
else:
self.parentScreen(self):
def parentScreen(self):
enter = raw_input("ENTER [1] to go to CHILD class:")
self.goTo(enter)
p = PARENT()
c = CHILD()
Okay, so to my beginner eyes this conceptually should work. I imported the class CHILD and created a reference to it "c = CHILD". This concept works when both class's are in the same file but not when they are in two different files. Why?
Instead of importing CHILD from child and storing it as a reference it instead executes immediately and does not initiate the PARENT class. Why doesn't this work?
I have seen people reference the whole 'name' == 'main' but I don't really know how to implement that and I feel as if there is an easier way.
Any help would be appreciated. Thank you!