如何在迭代中更改类 self.variable [关闭]

问题描述 投票:0回答:0

如何通过列表在迭代中更改类 self.variable?

Class:
    self.white_queen_castling_currently = True
    self.white_king_castling_currently = True
    self.black_queen_castling_currently = True
    self.black_king_castling_currently = True

    self.all_castlings = {'wQ': self.white_queen_castling_currently, 'wK': self.white_king_castling_currently,
                           'bQ': self.black_queen_castling_currently, 'bK': self.black_king_castling_currently}

    def castling_under_attack(self):  
        castlings = [([(7, 2), (7, 3)], 'wQ'), ([(7, 5), (7, 6)], 'wK'), ([(0, 2), (0, 3)], 'bQ'), ([(0, 5), (0, 6)], 'bK')]
    
        for castling in castlings:
            squares = castling[0]
            for square in squares:
                if self.board[square[0]][square[1]] != '--' or (move.end_row == square[0] and move.end_column == square[1]):
                    self.all_castlings[castling[1]] = False


没有错误,但是类 self.variable 没有改变。

我也试过这样说:

    castlings = [([(7, 2), (7, 3)], self.white_queen_castling_currently), ([(7, 5), (7, 6)], self.white_king_castling_currently),
                 ([(0, 2), (0, 3)], self.black_queen_castling_currently), ([(0, 5), (0, 6)], self.black_king_castling_currently)]

但也不起作用

python class variables iteration self
© www.soinside.com 2019 - 2024. All rights reserved.