Welcome to the definitive pedagogical guide on Python Methods. This document provides an end-to-end learning pathβfrom fundamental instance methods and decorators (@classmethod, @staticmethod, @property) to special dunder methods, the descriptor protocol, Method Resolution Order (MRO), and version-specific CPython behaviors (Python 2.7 through Python 3.13).
- Introduction to Methods vs. Standalone Functions
- Instance Methods &
selfBinding - Class Methods (
@classmethod) & Factory Pattern - Static Methods (
@staticmethod) & Utility Isolation - Property Methods (
@property,@setter,@deleter) - Special Dunder Methods & Operator Overloading
- The Descriptor Protocol (
__get__,__set__,__delete__) - Method Resolution Order (MRO) & C3 Linearization
- Runtime Method Introspection (
dir(),getattr(),inspect) - Cross-Version Behavioral Analysis (Python 2.7 to 3.13)
- 10 Practical Implementation Examples
- Common Method Pitfalls & How to Avoid Them
- Comparative Matrix: Functions vs. Method Types
A method is a function bound to a class or an instance of a class. Unlike standalone functions defined at module scope, methods implicitly receive the bound object or class reference as their first argument (self or cls) when invoked via dot notation (instance.method()).
| Feature | Standalone Function | Instance Method | Class Method | Static Method |
|---|---|---|---|---|
| First Implicit Arg | None | self (Instance) |
cls (Class) |
None |
| Decorator | None | None | @classmethod |
@staticmethod |
| Access Scope | Global / Module | Instance & Class State | Class State Only | No Bound State |
| Descriptor Mechanism | Standard Function | Bound Method Descriptor | Class Method Descriptor | Static Method Descriptor |
Instance methods are the most common type of method in Python OOP. They accept self as their first parameter, granting access to instance attributes (self.attr) and other instance methods.
class BankAccount:
def __init__(self, account_holder: str, initial_balance: float = 0.0) -> None:
self.account_holder = account_holder
self._balance = float(initial_balance)
def deposit(self, amount: float) -> float:
"""Instance method modifying instance balance state."""
if amount <= 0:
raise ValueError("Deposit must be positive")
self._balance += amount
return self._balanceClass methods take cls as their first argument instead of self. They are bound to the class itself and are frequently used for alternative constructors (factory patterns) or class-level state updates.
class UserProfile:
total_users = 0
def __init__(self, username: str, role: str) -> None:
self.username = username
self.role = role
UserProfile.total_users += 1
@classmethod
def from_csv(cls, csv_str: str) -> "UserProfile":
"""Class method alternative constructor."""
username, role = csv_str.split(",")
return cls(username=username.strip(), role=role.strip())Static methods do not receive self or cls. They behave like regular functions, but live inside the class namespace for logical grouping.
class UserProfile:
@staticmethod
def validate_username(username: str) -> bool:
"""Static method: No access to instance or class state."""
return len(username.strip()) >= 3Properties convert methods into managed attributes with controlled getter, setter, and deleter behavior.
class StudentGrade:
def __init__(self, score: float = 0.0) -> None:
self._score = 0.0
self.score = score
@property
def score(self) -> float:
return self._score
@score.setter
def score(self, value: float) -> None:
if not (0.0 <= value <= 100.0):
raise ValueError("Score must be between 0 and 100")
self._score = float(value)
@score.deleter
def score(self) -> None:
self._score = 0.0Special methods (surrounded by double underscores) enable object customization and operator overloading (+, ==, len(), repr(), call()).
class Vector2D:
def __init__(self, x: float, y: float) -> None:
self.x = x
self.y = y
def __str__(self) -> str:
return f"Vector2D({self.x}, {self.y})"
def __add__(self, other: "Vector2D") -> "Vector2D":
return Vector2D(self.x + other.x, self.y + other.y)
def __eq__(self, other: object) -> bool:
if not isinstance(other, Vector2D):
return False
return self.x == other.x and self.y == other.yThe descriptor protocol powers @property, @classmethod, and @staticmethod behind the scenes. Any object defining __get__(), __set__(), or __delete__() acts as a descriptor.
class NonNegative:
def __init__(self, name: str) -> None:
self.name = name
def __get__(self, instance: object, owner: type) -> float:
if instance is None:
return self
return instance.__dict__.get(self.name, 0.0)
def __set__(self, instance: object, value: float) -> None:
if value < 0:
raise ValueError(f"{self.name} cannot be negative")
instance.__dict__[self.name] = float(value)When calling a method in Python inheritance hierarchies, CPython uses C3 Linearization to search classes in order (Class.mro() or Class.__mro__).
class Base:
def action(self): return "Base"
class MixinA(Base):
def action(self): return "MixinA -> " + super().action()
class MixinB(Base):
def action(self): return "MixinB -> " + super().action()
class Child(MixinA, MixinB):
pass
print(Child.mro())
# Output: [Child, MixinA, MixinB, Base, object]Runtime introspection enables dynamic method discovery and invocation:
import inspect
class Demo:
def greet(self): return "Hello"
d = Demo()
print(callable(getattr(d, "greet"))) # True
print(inspect.ismethod(d.greet)) # True
print(inspect.isfunction(Demo.greet))# True (Python 3)Python 2.7 βββββββββββββββββββΊ Python 3.3 - 3.8 ββββββββββΊ Python 3.11 - 3.13
Unbound Methods (instancemethod) Plain Functions in Class Dict Vectorcall Protocol &
class Foo(object): required Implicit super(), PEP 487 15-25% Method Call Speedup
- Python 2.7: Calling
Foo.barreturned anunbound methodobject requiringisinstancechecks againstFoo. Classes had to explicitly inherit fromobject(class Foo(object):). - Python 3.3+: Unbound methods eliminated;
Foo.barreturns a standardfunction. Zero-argumentsuper()introduced. - Python 3.8β3.13: CPython introduced the Vectorcall Protocol (PEP 590), avoiding temporary tuple/dict allocations during method calls, resulting in 15β25% faster method execution.
class Account:
def __init__(self, name: str, balance: float) -> None:
self.name = name
self._balance = balance
def deposit(self, amount: float) -> float:
self._balance += amount
return self._balanceclass Date:
def __init__(self, year: int, month: int, day: int) -> None:
self.year, self.month, self.day = year, month, day
@classmethod
def from_iso(cls, iso_str: str) -> "Date":
y, m, d = map(int, iso_str.split("-"))
return cls(y, m, d)class Validator:
@staticmethod
def is_email(email: str) -> bool:
return "@" in email and "." in emailclass Temperature:
def __init__(self, celsius: float = 0.0) -> None:
self.celsius = celsius
@property
def fahrenheit(self) -> float:
return (self.celsius * 9 / 5) + 32class Multiplier:
def __init__(self, factor: float) -> None:
self.factor = factor
def __call__(self, value: float) -> float:
return value * self.factor-
Forgetting
selforclsas First Parameter:- Error:
TypeError: method() takes 0 positional arguments but 1 was given - Fix: Always declare
selffor instance methods orclsfor class methods.
- Error:
-
Using
@staticmethodWhen@classmethodis Required:- Static methods cannot construct new instances dynamically or access class attributes.
-
Mutable Class Attributes Shared Across Instances:
- Class attributes like
roles = []are shared across all instances. Initialize lists inside__init__for instance isolation.
- Class attributes like
| Metric | Standalone Function | Instance Method | Class Method | Static Method |
|---|---|---|---|---|
| Binding | Unbound | Bound to Instance | Bound to Class | Unbound |
| Memory Overhead | Minimal | Small (method wrapper) | Small (method wrapper) | Minimal |
| Primary Use Case | Independent Utilities | Encapsulated Logic | Factory / Class State | Grouped Utilities |