我是否必须在python 2.7中实现所有抽象方法?

Zac*_*ach 3 python oop pycharm python-2.7

我适应文字冒险游戏教程,github上,以适合蟒蛇2.7.我正在为我的IDE使用PyCharm 4.5.4社区版.当我不覆盖父方法时,它给我一个错误:

类WolfRoom必须实现所有抽象方法

首先要摆脱这个错误,我定义了丢失的方法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
Run Code Online (Sandbox Code Playgroud)

...

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()
Run Code Online (Sandbox Code Playgroud)

...

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.
            """
Run Code Online (Sandbox Code Playgroud)

Cox*_*oxy 9

我相信这实际上是由于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()
Run Code Online (Sandbox Code Playgroud)

上面的代码成功地将0输出到输出,因为当Python无法在GrandChild中找到foo()的实现时,它会查找继承链并在Child中找到它.但是,出于某种原因,PyCharm检查器期望所有引发NotImplementedError的类都在继承链的所有级别中实现.

如果你在具有大型继承结构的程序中遵循这种风格,那么当你不需要时,你会发现自己非常冗长,通过实现方法并在整个链中调用super.就个人而言,我只是忽略了错误,并认为PyCharm应该更新为不显示它,如果它找到在它正在检查的类的任何超类中实现的方法.

  • 请参阅此处的错误报告:https://youtrack.jetbrains.com/issue/PY-16776在5.0.x中标记为已修复.我有5.0.1,它仍然是一个问题,所以我猜这个修复即将到来. (3认同)