1
votes

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!

1

1 Answers

1
votes

You are importing the CHILD class properly, but you are calling it from outside your PARENT class. The PARENT class thinks that the variable c is a local variable to the function goTo. You could use a global variable c, but anyone would tell you that that is a big no no.

To answer your other question you probably have some code that executes in CHILD. If you only want this code to run when you run the file in which the CHILD class then put it after a

if __name__ == '__main__': 

This only allows the code preceding it to run if executed directly and it will not run if you import the class. see examples below.

You can just create an instance variable of the CHILD class in your __init__ and use it in the rest of your PARENT class.

class PARENT(object):

    def _init_(self):

        print "START PARENT CLASS"
        self.c = CHILD() # create instance of CHILD 

    def goTo(self,enter):

        if enter == "1":

           self.c.childScreen() # then you can access CHILD class like this

        else:

           self.parentScreen(self):

    def parentScreen(self):

        enter = raw_input("ENTER [1] to go to CHILD class:")
        self.goTo(enter)


if __name__ == '__main__':
     p = PARENT()

or you can actaully inherit CHILD into PARENT:

class PARENT(CHILD):

    def _init_(self):

        print "START PARENT CLASS"


    def goTo(self,enter):

        if enter == "1":

           # now you can access the CHILD functions as if the we were coded in the
           # PARENT class
           self.childScreen()

        else:

           self.parentScreen(self):

    def parentScreen(self):

        enter = raw_input("ENTER [1] to go to CHILD class:")
        self.goTo(enter)

if __name__ == '__main__':
    p = PARENT()