Python requires self as the explicit first parameter in instance method definitions because Python translates method calls like obj.method(arg) into plain function calls on the class—ClassName.method(obj, arg)—via the descriptor protocol, ensuring explicit visibility of instance state (self.attr) over local variables without hidden scoping rules.

Basic Example

class BankAccount:
    def __init__(self, owner: str, balance: float = 0.0) -> None:
        self.owner = owner        # Binds attribute to the specific instance
        self.balance = balance

    def deposit(self, amount: float) -> float:
        self.balance += amount
        return self.balance

account = BankAccount("Amina", 100.0)

# 1. Recommended: Standard bound method call (Python passes `account` as `self` automatically)
print(account.deposit(50.0))
# Output: 150.0

# 2. Equivalent unbound function call on the class (reveals what Python does under the hood):
print(BankAccount.deposit(account, 25.0))
# Output: 175.0

Key Details & Edge Cases

  • How Python binds self automatically (the Descriptor Protocol):
    • Functions defined inside a class body are ordinary function objects stored in ClassName.__dict__. Because Python functions implement __get__, accessing account.deposit invokes FunctionType.__get__(account, BankAccount), returning a lightweight bound method wrapper that prepends account as the first positional argument (self) when called.
  • Explicit self eliminates ambiguity between local variables and instance attributes:
    • Unlike C++ or Java—where an unqualified balance = 10 inside a method can implicitly mutate a member field—Python has no variable declaration keywords (var/let). Requiring self.balance makes it unmistakably clear whether you are creating a temporary local variable (balance = 10) or mutating instance state (self.balance = 10).
  • Why calling a method without self in the definition raises TypeError:
    • Defining def deposit(amount): and calling account.deposit(50) passes two arguments (account and 50) to a function expecting one, raising TypeError: BankAccount.deposit() takes 1 positional argument but 2 were given.
  • When self is NOT used (@classmethod vs. @staticmethod):
    • @classmethod: Receives the class itself (cls) as its first argument instead of an instance—used for alternative constructors (BankAccount.from_dict(data)).
    • @staticmethod: Receives neither self nor cls—behaves like a plain function namespaced inside the class.
  • self is a strong PEP 8 convention, not a reserved keyword:
    • Python syntactically allows any valid identifier name (e.g., this), though PEP 8 strictly mandates self for instance methods and cls for class methods so linters, IDEs, and other Python developers immediately recognize method signatures.

Comparison of Method Types in a Python Class

Decorator / Method Type Implicit First Argument Passed by Python Can Access/Modify Instance Attributes (self.x)? Can Access/Modify Class Attributes (cls.x)? Best Used When
Regular Instance Method (no decorator) self (the instance) Yes Yes (via self.__class__ or ClassName) Reading or mutating state belonging to a specific object instance.
@classmethod cls (the class object) No (no instance exists yet) Yes Alternative constructors (e.g., from_json()) or factory methods that respect subclassing.
@staticmethod None No No (unless hardcoded by class name) Utility functions logically grouped with the class that depend on neither instance nor class state.