You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Board内部的Chess类中访问外部类的self.grid列表?

问题:嵌套Chess类如何访问Board类的self.grid?

我正在用Pygame开发带棋子的棋盘,为了代码整洁,创建了Board类,并在其中嵌套Chess类存放棋类相关代码。Chess类的test函数需要为棋盘网格的每个方块分配棋子,请问如何在该函数中使用Board类的self.grid列表?

我的代码如下:

class Board:
    def __init__(self, size):
        self.size = size
        self.grid = []
        for y in range(self.size):
            for x in range(self.size):
                self.grid.append(Square(x, y, self.size))

    
    class Chess:
        order = ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r',
                 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p', 
                 '-', '-', '-', '-', '-', '-', '-', '-', 
                 '-', '-', '-', '-', '-', '-', '-', '-', 
                 '-', '-', '-', '-', '-', '-', '-', '-', 
                 '-', '-', '-', '-', '-', '-', '-', '-', 
                 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P',
                 'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']
        
        def test(self):
            for square in self.grid:
                square.type = Board.Chess.order[self.grid.index(square)]

我希望能这样调用:

board = Board(8)
board.Chess.test()

解决方案

问题根源

当前嵌套的Chess类没有持有Board实例的引用,且test作为实例方法,直接通过board.Chess.test()调用时无法正确获取Board的self.grid(此时self不是Board对象)。另外用self.grid.index(square)获取索引效率极低,会重复遍历列表。

方案一:让Chess实例关联Board实例(推荐)

将Chess类作为Board的属性实例化,在Chess的构造方法中保存Board实例的引用:

class Board:
    def __init__(self, size):
        self.size = size
        self.grid = []
        for y in range(self.size):
            for x in range(self.size):
                self.grid.append(Square(x, y, self.size))
        # 实例化Chess并传入当前Board实例
        self.chess = self.Chess(self)
    
    class Chess:
        order = ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r',
                 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p', 
                 '-', '-', '-', '-', '-', '-', '-', '-', 
                 '-', '-', '-', '-', '-', '-', '-', '-', 
                 '-', '-', '-', '-', '-', '-', '-', '-', 
                 '-', '-', '-', '-', '-', '-', '-', '-', 
                 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P',
                 'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']
        
        def __init__(self, board):
            # 保存Board实例的引用
            self.board = board
        
        def test(self):
            # 通过board引用访问grid,用enumerate直接获取索引更高效
            for idx, square in enumerate(self.board.grid):
                square.type = self.order[idx]

调用方式:

board = Board(8)
board.chess.test()

方案二:将test改为类方法,传入Board实例

如果坚持要使用类似board.Chess.test()的调用方式,可以把test改为类方法,显式传入Board实例:

class Board:
    def __init__(self, size):
        self.size = size
        self.grid = []
        for y in range(self.size):
            for x in range(self.size):
                self.grid.append(Square(x, y, self.size))
    
    class Chess:
        order = ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r',
                 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p', 
                 '-', '-', '-', '-', '-', '-', '-', '-', 
                 '-', '-', '-', '-', '-', '-', '-', '-', 
                 '-', '-', '-', '-', '-', '-', '-', '-', 
                 '-', '-', '-', '-', '-', '-', '-', '-', 
                 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P',
                 'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']
        
        @classmethod
        def test(cls, board):
            for idx, square in enumerate(board.grid):
                square.type = cls.order[idx]

调用方式:

board = Board(8)
Board.Chess.test(board)
# 或 board.Chess.test(board) 也可正常运行

内容的提问来源于stack exchange,提问作者PogoSpin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 05:20:31