0

I'm trying to use a variable that's defined in class A in class B. Basically I need the users entry in class A to be the file name as data I save in class B Here's my code:

class A(object):

    def __init__(self, master):
       self.master = master
       self.labelSub=Label(self.master, text="Participant No.")  #where users their name
       self.entrySub=Entry(self.master,bg="grey")
       A.csv_name_sub = str(self.entrySub.get()) #save users entry

class B(A):

    def __init__(self, master):
       self.master = master
       A.csv_name_sub = str(self.entrySub.get())
       self.resultFile = open("/Users/Desktop/" + A.csv_name_sub +
                           '_results.csv', 'w') #use the users entry as the name of the csv file I save

But error tells me "AttributeError: 'B' object has no attribute 'entrySub'". Could you help me with that? Thanks!!

key
  • 63
  • 3
  • 9
  • Make it a global variable by adding `Global` to the beginning of the class with the variable. Like `Global A` – Pike D. Feb 12 '17 at 22:56

2 Answers2

2

Inside B.__init__, A.__init__ hasn't been called, so self.entrySub hasn't been defined.

This thread (Understanding Python super with init methods) might help you.

You could replace the first 2 lines of B.__init__ with a call to super.

Note : are you sure you want to mix class and instance variables like this? There's only one A.csv_name_sub for all A objects, but it seems to depend on master, which could be different for every A object.

Community
  • 1
  • 1
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • Hi Eric, thanks! There are other objects on class A and class B. I didn't show all of them since I want to keep my code concise. They are all on the same master. – key Feb 13 '17 at 00:45
  • I used `class ChildA` `Base.__init__(self)` in the page you suggested (also Michael suggested below). But it didn't work properly. The AttributeError didn't appear, and the program ran, but the file name didn't change according with the entry. Here's my code `class B(A): def __init__(self, master):` `A.__init__(self, master)`,` A.csv_name_sub = str(self.entrySub.get())`, `self.resultFile = open("/Users/Desktop/" + A.csv_name_sub+ '_results.csv', 'w')`. The file name I got is only "_results.csv". Do you know why? – key Feb 13 '17 at 00:45
0

The constructor of the inherited class A is not automatically called: https://docs.python.org/3.5/reference/datamodel.html?highlight=baseclass#object.init

After callling the base class constructor, your instance of B should have the attribute:

class B(A):

    def __init__(self, master):
        A.__init__(self, master)
        ...

See https://docs.python.org/3.5/tutorial/classes.html#class-and-instance-variables explaining the difference between class variables and instance variables.

Edit: Eric edited faster. ;)

Michael
  • 318
  • 2
  • 7
  • Hi Michael, thanks for your reply. I tried your code, and no error presents (program ran), but the file name is not saved as in `csv_name_sub+_results.csv`. Here's my code `class B(A): def __init__(self, master):` `A.__init__(self, master)`, `A.csv_name_sub = str(self.entrySub.get())`, `self.resultFile = open("/Users/Desktop/" + A.csv_name_sub+ '_results.csv', 'w')`. It didn't say the csv_name_sub doesn't exist, but just didn't show in the file name. Do you know why? Thx again! – key Feb 13 '17 at 00:38