我正在调整 text adventure game tutorial , github , 以适应 python 2.7。我正在为我的 IDE 使用 PyCharm 4.5.4 社区版。当我不覆盖父方法时,它会给我一个错误:
Class WolfRoom must implement all abstract methods
起初为了摆脱这个错误,我将缺少的方法 def modify_player(self, the_player):
定义为 pass
但我很快意识到我正在重写该方法没有什么不是我想要的。现在,如果我只是从 WolfRoom 类中删除该方法,则会出现 IDE 错误,如上所示,但当我运行我的游戏时它似乎工作正常。我应该保留此方法还是定义它并使用 super()
?
下面是一些代码片段:
class MapTile(object):
"""The base class for all Map Tiles"""
def __init__(self, x, y):
"""Creates a new tile.
Attributes:
:param x: The x coordinate of the tile.
:param y: The y coordinate of the tile.
"""
self.x = x
self.y = y
def intro_text(self):
"""Information to be displayed when the player moves into this tile."""
raise NotImplementedError()
def modify_player(self, the_player):
"""Process actions that change the state of the player."""
raise NotImplementedError()
def adjacent_moves(self):
"""Returns all move actions for adjacent tiles."""
moves = []
if world.tile_exists(self.x + 1, self.y):
moves.append(actions.MoveEast())
if world.tile_exists(self.x - 1, self.y):
moves.append(actions.MoveWest())
if world.tile_exists(self.x, self.y - 1):
moves.append(actions.MoveNorth())
if world.tile_exists(self.x, self.y + 1):
moves.append(actions.MoveSouth())
return moves
def available_actions(self):
"""Returns all of the available actions in this room"""
moves = self.adjacent_moves()
moves.append(actions.ViewInventory())
return moves
...
class EnemyRoom(MapTile):
def __init__(self, x, y, enemy):
self.enemy = enemy
super(EnemyRoom, self).__init__(x, y)
def intro_text(self):
pass
def modify_player(self, the_player):
if self.enemy.is_alive():
the_player.hp = the_player.hp - self.enemy.damage
print("Enemy does {} damage. You have {} HP remaining.".format(self.enemy.damage, the_player.hp))
def available_actions(self):
if self.enemy.is_alive():
return [actions.Flee(tile=self), actions.Attack(enemy=self.enemy)]
else:
return self.adjacent_moves()
...
class WolfRoom(EnemyRoom):
def __init__(self, x, y):
super(WolfRoom, self).__init__(x, y, enemies.Wolf())
def intro_text(self):
if self.enemy.is_alive():
return """
A grey wolf blocks your path. His lips curl to expose canines as white as
the nights sky. He crouches and prepares to lunge.
"""
else:
return"""
The corpse of a grey wolf lays rotting on the ground.
"""
最佳答案
我相信这实际上是由于 PyCharm 检查器在查看是否有任何未实现的方法会引发 NotImplementedError 时犯了一个错误,或者至少是一个关于 PEP 8 风格的可疑决定。考虑这个非常相似的简单示例:
class Base(object):
def foo(self):
raise NotImplementedError
def bar(self):
return 0
class Child(Base):
def foo(self):
return 0
class GrandChild(Child):
def bar(self):
return 1
my_grand_child = GrandChild()
print my_grand_child.foo()
上面的代码成功地向输出打印了一个 0,因为当 Python 在 GrandChild 中找不到 foo() 的实现时,它会查找继承链并在 Child 中找到它。但是,出于某种原因,PyCharm 检查器希望所有引发 NotImplementedError 的类都在继承链的所有级别中实现。
如果您在具有大型继承结构的程序中遵循这种风格,您会发现自己在整个链中实现方法和调用 super 非常冗长,而实际上根本不需要这样做。就个人而言,我只是忽略该错误,并认为 PyCharm 应该更新为在它发现它正在检查的类的任何父类(super class)中实现的方法时不显示它。
关于python - 我必须在 python 2.7 中实现所有抽象方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32879684/