我正在尝试用 python 制作康威的生命游戏。基本上,我需要制作一个网格/二维列表并用值填充它。我将网格注册为网格对象。对象指针是Game对象的一个属性。
由于某种原因,当我尝试在网格上运行实例方法时,python 说“setCell 需要 3 个位置参数,但得到了 4 个”。但我只给了3个。参见下面的代码:
我认为这可能是“self”变量的问题,但使用“tempgrid”包装代理来保存网格似乎不起作用。 (虚线代表单独的文件)
class Cell:
def __init__(self, xpos, ypos, status):
self.xpos = xpos
self.ypos = ypos
self.status = status
----------------
from .cell import Cell
class Grid:
def __init__(self, size):
self.size = size
self.cells = [[None for _ in range(size)] for _ in range(size)]
def setCell(xpos, ypos, status):
cells[xpos][ypos] = Cell(xpos, ypos, status)
------------------------
from .grid import Grid
class Game:
def __init__(self, size):
self.grid = Grid(size)
for i in range(size):
for j in range(size):
self.grid.setCell(i, j, 0) #python says this is supposed to have 3 arguments but has 4, even though I give 3
发生什么事了?作为引用,确切的错误消息是“TypeError:setCell() 需要 3 个位置参数,但给出了 4 个”
最佳答案
您忘记了 self
def setCell(self, xpos, ypos, status):
当你运行时
self.grid.setCell(i, j, 0)
然后Python运行
Grid.setCell(self.grid, i, j, 0)
所以定义中需要 4 个参数
(然后它可以将 self.grid
分配给 self
)
关于python - 从初始化程序运行实例方法时出现神秘的 "extra argument"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56879808/