self (Python)

self is the first parameter of a method in a Python class, and it refers to the specific object the method is being called on. It lets methods read and change that object's own data.

Python

You don't pass self in manually when calling a method — Python supplies it automatically. By convention it is always named self, though technically any name would work.

Examples

Python

class Counter:
    def __init__(self):
        self.count = 0
    def add(self):
        self.count += 1

c = Counter()
c.add()
c.add()
print(c.count)

Output:

2

Learn it in a lesson

See also

Back to the Dictionary