Methods and receivers
Call a method on its dictionary
A method is a function stored in a collection. The receiver is the collection the method works on. Dot access supplies that receiver when the function has self as its first required parameter.
func describe(self):
return 'Hello, {self.name}!'
player = {name = 'Ada', greet = describe}
print(player.greet())
Hello, Ada!
The call supplies player as self, so the function can read the name field from that dictionary.
Keep a method for later
Saving the value reached through a dot keeps its receiver. This is useful when another part of the program will call it later.
func describe(self):
return self.name
player = {name = 'Ada', describe = describe}
announce = player.describe
print(announce())
Ada
announce still uses player as its receiver. Accessing the function by key instead returns the stored function without binding a receiver.
Dictionary and hybrid member access binds a receiver when a script function declares self as its first required parameter. A method extracted with dot access keeps that receiver. Indexing returns the stored function directly, so the caller supplies the receiver when invoking it. Module access also returns the stored function directly.