在Python中初始化新实例

时间:2016-02-26 14:22:31

标签: python

我正在为Python上的tic-tat-toe编写单元测试。当我注意到我的游戏对象并没有为每个方法重新实现自己时,我一直非常困惑。

这就是我所说的:

def test_satisfactory_field_occupation(self):
        play = tictactoe.Play()
        play.make_move("+", 1, 1)
        self.assertEqual(play.check_satisfaction(1, 1), "Field has been already occupied, try again")

def test_satisfactory_success(self):
        play = tictactoe.Play()
        self.assertEqual(play.check_satisfaction(1, 1), "Ok") 

我发现异常:

FAIL: test_satisfactory_success (__main__.TestPlay)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/sergei_rudenkov/PycharmProjects/hello_world/tic-tac-toe/tictactoe_test.py", line 23, in test_satisfactory_success
    self.assertEqual(play.check_satisfaction(1, 1), "Ok")
AssertionError: 'Field has been already occupied, try again' != 'Ok' 

Play类是:

class Play(object):
    game = [['-', '-', '-'],
            ['-', '-', '-'],
            ['-', '-', '-']]

    move_count = 1
    finished = False

    def __str__(self):
        return "\n".join(map(str, self.game))

    def check_finished(self):
        result = False
        for i in range(2):
            if self.game[i][0] == self.game[i][1] == self.game[i][2] != '-':
                result = self.game[i][0]
            elif self.game[0][i] == self.game[1][i] == self.game[2][i] != '-':
                result = self.game[i][0]
        if self.game[0][0] == self.game[1][1] == self.game[2][2] != '-':
            return self.game[0][0]
        elif self.game[0][2] == self.game[1][1] == self.game[2][0] != '-':
            return self.game[0][2]
        elif not any("-" in row for row in self.game):
            return "Draw"
        else:
            return result

    def make_move(self, sign, x, y):
        self.game[x][y] = sign
        self.move_count += 1
        self.finished = self.check_finished()
        print self

    def check_satisfaction(self, x, y):
        try:
            x, y = int(x), int(y)
        except ValueError:
            return "Please enter integers, try again"
        if not (0 <= x <= 2 and 0 <= y <= 2):
            return "Arguments greater then 2 or less then 0 are not allowed, try again"
        if self.game[x][y] != '-':
            return "Field has been already occupied, try again"
        return "Ok"

    def winner(self):
        if self.finished == '+':
            return "First player (+) has won!"
        elif self.finished == '0':
            return "Second player (0) has won!"
        elif self.finished == 'Draw':
            return "The result is draw!"

请正确理解我:我来自java并且被认为每种方法都拥有它自己的堆栈但我所看到的高度惊讶我。有人可以帮我理解发生了什么吗?

2 个答案:

答案 0 :(得分:4)

您宣布列出&#34;游戏&#34; as&#34;静态&#34;。 意味着每个实例将共享相同的列表。移动游戏&#34;在构造函数中声明,然后你应该没问题。

class Play(object):
   def __init__(self):    

        self.game = [['-', '-', '-'],
            ['-', '-', '-'],
            ['-', '-', '-']]

原因是当您在类级别声明列表时,该列表会在解析时分配,当它们生成&#34;类对象时,您可以使用Play.game访问游戏列表。这应该已经让你了解游戏列表的范围。这里是一个简化的示例,说明在类级别声明的列表:

class Play:
    game =[0]


p1 = Play()
print p1.game
p1.game[0] =1
p2 = Play()
print p2.game

答案 1 :(得分:4)

class Play(object):
    def __init__(self):
        self.game = [['-', '-', '-'],
                    ['-', '-', '-'],
                    ['-', '-', '-']]

        self.move_count = 1
        self.finished = False

确保在该类的所有其他方法中使用self.前缀始终访问这些成员变量:

  • self.game
  • self.move_count
  • self.finished

查看Python tutorial: 9.3.5. Class and Instance Variables