我真的不明白我做错了什么,因为当我在“小规模”尝试时,它是有效的。
我有一个名为Play()
的类
代码如下:
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
接下来我有另一个名为Score()
的类。
class Score(Play):
def __init__(self):
self.initialScore = 0
def letterGuess(self):
self.initialScore += 1
return self.errList
我实例化了两个对象:
game = Play()
points = Score()
如果我这样做:
print points.letterGuess()
它给我报错了:
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'
我不明白为什么我可以轻松做到这一点:
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]
我对面向对象编程非常陌生,可能会犯各种愚蠢的错误,但我无法找出问题所在!
我认为我已经发布了所有相关的代码,但如果你认为错误可能出现在其他地方,我可以提供它。
正如我所说,我很陌生,所以这可能与继承无关,我只是认为当你从另一个类中获取“某些东西”时,它被称为继承(你现在一定在屏幕前大喊大叫)
return Play.errlist
吗? - Matt Habel