我正在通过Zed Shaw的LearnPythontheHardWay工作,而且我很难过。在Engine
类下next_scene_name = current_scene.enter()
:Learn Python the Hard Way Example#43
,我发现了错误:
[Attribute Error:'NoneType' object has no attribute 'enter']
在该行。
from sys import exit
class Scene(object):
def enter(self):
print "This scene is not yet configured. Subclass it and implement\
enter()."
exit(1)
class Engine(object):
def __init__(self, scene_map):
self.scene_map = scene_map
def play(self):
current_scene = self.scene_map.opening_scene()
last_scene = self.scene_map.next_scene('finished')
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
current_scene.enter()
class EmptyScene(Scene):
def enter(self):
pass
class FinishScene(Scene):
def enter(self):
pass
class Map(object):
scenes = {
'empty_scene': EmptyScene(),
'finished': FinishScene(),
}
def __init__(self, start_scene):
self.start_scene = start_scene
def next_scene(self, scene_name):
val = Map.scenes.get(scene_name)
return val
def opening_scene(self):
return self.next_scene(self.start_scene)
a_map = Map('empty_scene')
a_game = Engine(a_map)
a_game.play()`
的可能重复的[Python的:属性错误 - 'NoneType' 对象没有属性 '东西'](http://stackoverflow.com/questions/8949252/python-attribute-error-nonetype-object -has-no-attribute-something) – dimo414
@ dimo414我不同意,从最初的标题看来它似乎是重复的,但问题比您提出的问题更广泛。 – randomusername