self
noun
say it: “SELF” /sɛlf/
The first parameter of a method in a Python class: the object the method is working on, which Python passes in for you.
What is self?
self is the object a method is working on. Python passes it in automatically, so self.balance means "this account's balance".
class BankAccount:
def __init__(self, balance):
self.balance = balance
def deposit(self, amount):
self.balance += amount # this account's balance
In a sentence
- “self.balance means this account's balance, not every account's.”
- “Forgetting self in def deposit(amount) made Python complain about too many arguments.”
Level: Journeyman · More Python terms at this level
Updated