Error: "x instance has no y attribute" when trying to inherit from class

I cannot understand what I am doing wrong, because when I try to use it on a "small scale", it works there.

I have a class called Play()

I go like this:

 class Play(): def __init__(self): file = open("/home/trufa/Desktop/test", "r") self.word = random.choice(file.readlines()).rstrip() self.errAllowed = 7 self.errMade = 0 self.errList = [] self.cheatsAllowed = 2##chetas not incrementing self.cheatsMade =0 self.wordList = ["*"]*len(self.word) ##this one is the one I want to have available in another class 

...

Then I have another class called Score()

 class Score(Play): def __init__(self): self.initialScore = 0 def letterGuess(self): self.initialScore += 1 return self.errList 

...

I created both instances:

 game = Play() points = Score() 

And if I do this:

 print points.letterGuess() 

This gives me an error:

 Traceback (most recent call last): File "/home/trufa/workspace/hangpy/src/v2.py", line 188, in <module> startGame() File "/home/trufa/workspace/hangpy/src/v2.py", line 134, in startGame print points.letterGuess() File "/home/trufa/workspace/hangpy/src/v2.py", line 79, in letterGuess return self.errList AttributeError: Score instance has no attribute 'errList' 

I do not understand why, since I can do this without much trouble:

 class One(): def __init__(self): self.list= [1,2] class Two(One): def meth(self): return self.list uan = One() tu = Two() print uan.list print tu.meth() ## Both output [1,2] 

I am very new to OOP, so I can make all kinds of stupid mistakes, but I can't figure out where!

I think I posted all the relevant code, but I think the error may be in another place, I can provide it.

As I said, I'm very new, so this has nothing to do with inheritance. I just think this is called when you get โ€œsomethingโ€ from another class (you should now scream on the screen)

+4
source share
3 answers

You overwrite the original __init__ , which is then not called or initialized by the members. You should call the parent __init__ separately, usually with this snippet:

 def __init__(self): super(Score, self).__init__() 

See the docs for super() for more details. However, super() only works for so-called new-style classes. Therefore, you must either change the definition of Play inherit from object :

 class Play(object) 

or you call the parent method directly:

 def __init__(self): Play.__init__(self) 
+12
source

When you inherit the Play class, you automatically get the attributes that you created in the Play definition, but you don't get the attributes created in Play.__init__ . You should explicitly call it like this:

 class Score(Play): def __init__(self): Play.__init__(self) self.initialScore = 0 

See Boldewyn's suggestion for using super for this; but IMO, you should probably get used to the basic way of inheritance before starting with super .

For further clarification, if you do not redefine __init__ , as you have in this case, it is automatically inherited and called.

+3
source

You forgot to initialize the superclass.

 class Score(Play): def __init__(self): super(Score, self).__init__() self.initialScore = 0 
+2
source

All Articles