Methods and self
A method is a function inside a class. Its first parameter is self, which stands for the object the method was called on. You do not pass self yourself: Python does it. Methods can read and change the attributes of the object.
Example
class Counter:
def __init__(self):
self.count = 0
def add(self, n=1):
self.count += n
return self
def show(self):
return f"Count is {self.count}"
c = Counter()
c.add().add(5)
print(c.show())Result
Count is 6
Exercise
The first parameter of a method is...
def show(): pass
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.