0

有人可以解释初始化程序之后函数中发生了什么吗?

具体other.rowother.column。我以前从未见过这种情况,也不明白如何传递变量并使用它来引用实例属性。

class Window:
    def __init__(self, row: int, column: int):
        self.column = column
        self.row = row

    def __add__(self, other):
        row = self.row + other.row
        col = self.column + other.column
        return Tile(row, col)
4

1 回答 1

2

self是您从中调用方法的对象,other是第二个对象。键入obj1 + obj2会导致 python 在类中寻找__add__dunder 方法obj1。如果他找到它,他将作为第一个参数obj1(self),第二个参数是obj2(other)。然后他从 obj1(称为 self)和 obj2(称为 other)中获取行值。对象必须具有列和行变量,如果其中一个没有,则会引发异常。列也是如此

于 2021-11-12T16:59:43.810 回答