Could you please help me to find the problem? Python code that works:
class ParamWindow:
def __init__(self, b):
self.a = b
print self.a
params = ParamWindow(8)
print params.a
this prints 8 and 8. Ok. Then I do:
class ParamWindow:
def __init__(self, parent, b):
self = wx.Frame(parent = parent, id=-1, title="Parameters")
self.a = b
print self.a
params = ParamWindow(None, 8)
print params.a
and it says "ParamWindow instance has no attribute 'a'". Why has not it? I told him that self is Frame and then added a field "a" (no error at this point) but when i ask to print it (error at print line), it forgets that "a" exists... Where am I wrong? Thanks.
1 Answer 1
def __init__(self, parent, b):
self = wx.Frame(parent = parent, id=-1, title="Parameters")
Here you reassign self, so you end up having no reference to the ParamWindow instance any more! You should never do that! What are you trying to achieve?
self.a = b
Here, you assign a to self, which is now the Frame, not the ParamWindow. ParamWindow.a never gets defined and you get the error later on.
Maybe you want to inherit from Frame? If so, your code should look like this:
class ParamWindow(wx.Frame):
def __init__(self, parent, b):
# Initialize the superclass (wx.Frame).
super(ParamWindow, self).__init__(parent=parent, id=-1, title="Parameters")
self.a = b
print self.a
7 Comments
self now has nothing to do with the original instance. You can assign anything you like to it, but it won't affect the instance.