Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 285
Trufa Avatar asked May 26 '11 19:05

Trufa


People also ask

Can a class attribute can be used without an instance of that class?

while you can access class attributes using an instance it's not safe to do so. In python, the instance of a class is referred to by the keyword self. Using this keyword you can access not only all instance attributes but also the class attributes.

Do you need to inherit from object Python 3?

You don't need to inherit from object to have new style in python 3. All classes are new-style. You don't have to, but the "Porting Python code to Python 3" says that it's still valid: docs.python.org/py3k/howto/pyporting.html#subclass-object Also: docs.python.org/reference/…

Does a class attribute exists as a unique object regardless of how many instances of the class are?

Class Attributes. Instance attributes are owned by the specific instances of a class. That is, for two different instances, the instance attributes are usually different.


1 Answers

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)
like image 195
Boldewyn Avatar answered Oct 24 '22 16:10

Boldewyn