无法更改类属性

时间:2015-03-02 00:10:45

标签: python

类GameState:

def __init__(self):
    '''initializes the players turn'''
    self._player_turn = 'BLACK'

board = []
def create_board2(self):
    '''creates the board with the given number of rows and columns'''
    new_game = GameState()
    rows = []
    columns = []
    r = int(new_game.num_rows())
    c = int(new_game.num_columns())
    columns = [None] *c
    for row in range(c):
        columns[row] = ['.'] * r
    middle = len(columns) // 2
    columns[middle - 1][middle - 1] = 'B'
    columns[middle - 1][middle] = 'W'
    columns[middle][middle - 1] = 'W'
    columns[middle][middle] = 'B'
    new_board = self.board.append(columns)
    print(new_board)
    return new_board

它打印出来没有。我正在尝试创建一个列表的类属性。我需要将它用于各种功能。

1 个答案:

答案 0 :(得分:0)

您的方法返回None,因为您要返回append来电的返回值;这是None。您可以通过以下方式发现此行为:

print(type([].append(1)))

我认为您要做的是返回self.board,或将self.board分配到new_board另一行。