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

偶尔善良 提交于 2019-12-10 14:16:30

问题


I can't really understand what I'm doing wrong, since when I try it in "small scale" and it is working there.

I have a class named Play()

I goes 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 instantiated both:

game = Play()
points = Score()

And if I do:

print points.letterGuess()

It 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 don't understand why since I can do this without any 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'm very new to OOP so I could be doing all kinds of silly mistakes but I can't figure out where!

I think I have posted all the relevant code, but I you think the error might be elsewhere, I can provide it.

As I said I'm very new, so this might have nothing to do with inheritance I just think it called that when you get "something" from within another class (you must be shouting at the screen by now)


回答1:


You overwrite the original __init__, which is then never called and doesn't initialize the members. You must call the parent's __init__ separately, usually with this snippet:

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

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

class Play(object)

or you call the parent's method directly:

def __init__(self):
    Play.__init__(self)



回答2:


When you inherit from the class Play, you automatically get the attributes that you've created in the definition of Play, but you don't get the attributes that you've created in Play.__init__. You have to explicitly call it like so:

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

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

To further clarify, if you don't override __init__ as you have in this case, then it's inherited and called automatically.




回答3:


You forgot to initialize the superclass.

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


来源:https://stackoverflow.com/questions/6143693/error-x-instance-has-no-attribute-y-when-trying-to-inherit-from-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!