0

I'm trying to update a variable in classA and used the updated info in classB. Hence I have two questions: 1) How to update class variable in instance method? 2) How to use inherit variables from parent class?

The main idea of this program is that user enters the name, and the name var, its value csv_name_sub will be save and updated and used in class B. (i.e. used as file name)

classA(object):
    def __init__(self, master, csv_name_sub):
        self.entrySub = Entry(self.master,bg="grey") #user enters sth
        button1 = Button(self.master,text='NEXT', command=self.gotoPage1)

    def writeToFile(self):
        self.csv_name_sub = str(self.entrySub.get()) #save value to var

    def gotoPage1(self):
        self.writeToFile()
        root2=Toplevel(self.master)
        self.instPage1=classB(root2)

classB(classA):
   def __init__(self, master, csv_name_sub):
        classA.__init__(self, master, csv_name_sub)
        print(self.csv_name_sub)
        self.resultFile = open(
         "/Users/" + self.csv_name_sub,'w')
        self.resultFileWrite = csv.writer(self.resultFile)

def main():

    root = Tk()
    myApp = classA(root, csv_name_sub)
    root.mainloop()

But the error is:

    myApp = classA(root, csv_name_sub)
    NameError: name 'csv_name_sub' is not defined

I understand that csv_name_sub is created in the parent class, and its value is not inherited in the child class. But how can I access the variable value in the child class? Because the value of csv_name_sub is determined by what users entered in the parent class, I can't define it in the child class by myself.

Thanks for you help!

4
  • The error is because csv_name_sub is no where defined in the main. like root = Tk() you should define csv_name_sub. Commented May 24, 2017 at 7:12
  • I don't quite understand this I think it's due to the value of var csv_name_sub is not updated after user's entry and inherited to classB correctly How are you expecting a child class to be created and values to be inherited from parent? You are not creating a ClassB object. Commented May 24, 2017 at 7:15
  • @Pavan Thanks for your comment! Mind me I have confusing wording here. My main idea is that I want to use the value of var csv_name_sub created in classA in classB. I understand that its value will not be inherited in classB, because it's not created in classB. So how can I find a way to use csv_name_sub's value (created in parent class) in child class? Commented May 24, 2017 at 17:06
  • Hi the code shared has some syntax error please check How to ask a good question Your question should have a minimum code to reproduce the case. But anyway checkout my answer I hope it helps Commented May 25, 2017 at 4:23

1 Answer 1

1

In your code when you initialize classA in __init__ of class B, The csv_name_sub of classA is not set because it's not inside the __init__ code of classA

I have this code and it works:

class classA():
    def __init__(self, csv = ""):
        print "Inside classA init"

    def set_csv(self, csv):
        print "Setting csv to %s" %csv
        self.csv = csv

class classB(classA):
    def __init__(self,csv):
        print("Inside classB init")
        classA.__init__(self, csv)
        classA.set_csv(self, csv)
        print "class B csv after set_csv %s" %self.csv

my_class = classB("abc")

Output:

Inside classB init
Inside classA init
Setting csv to abc
class B csv after set_csv abc

This will work, unless you want to create 2 different classA and class B object and want then to replicate the value, In this case you have to modify the classB.__init__ so that it'll take a classA obj as an argument and initialize itself. (In you case be sure to call set_csv before creating a classB object from class A object) Or have a csv as a static variable of classA, and write a method to modify that variable. This variable can be accessed by the class/ inherited classes, and modifying in one class is replicated across all the classes. Note that this variable will not be tied to a class object, and although you can access the variable from any subclasses, trying to modifying the variable from the subclass will return in a new static variable associated with that class and the parent class variable is not changed. Ex:

class classA():
    csv = ""
    def __init__(self, csv = ""):
        print "Inside classA init"
        classA.csv = csv

class classB(classA):
    def __init__(self,csv):
        print "Inside classB init"
        classA.__init__(self, csv)

my_classA = classA("abc")
print classA.csv 
my_classB = classB("efg")
print classA.csv
print classB.csv   #same as classA's csv
classB.csv = "lmn" #inherited static csv is masked.
print classA.csv
print classB.csv   # no longer points to the classA's csv

Output:

Inside classA init
abc #initialized
Inside classB init
Inside classA init
efg #changed in Init of classB by calling init of classA
efg #classB.csv
efg #classA.csv after (classB.csv = "lmn")
lmn #classB.csv created a new static variable csv, masking the variable of classA

I hope this helps to resolve your problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.