The keyword self#
In dem Code oben hast du gesehen, dass die Methode act
als Parameter das Schlüsselwort self
erwartet.
All methods that belong to an object always receive this keyword as the first parameter.
Then, within the method, attributes and methods of the object itself can be accessed using this keyword.
Example:
Dieser Code
@player.register
def act(self):
self.direction = "right"
ist gleichbedeutend mit diesem Code:
@player.register
def act(self):
player.direction = "right"
self
refers to the player
object where the method was registered.