Can someone explain what is happening in the function after the initialiser?
Specifically other.row
and other.column
. I have never seen this before and don't understand how you can pass a variable and use it to reference instance attributes.
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)
CodePudding user response:
self
is an object from which you are calling the method, other
is the second object. Typing obj1 obj2
will cause that python is looking for __add__
dunder method in the obj1
class. If he finds it, he takes as first argument obj1(self)
, the second argument is obj2(other)
. Then he takes the row value from obj1(called self) and row value from obj2(called other). Objects must have column and row variable, if one of them don't have it, an exception will be raised. The same goes for column