Objects and behaviour
What is a method?
On this page
A method is an operation associated with a type or object. When Python evaluates cart.add(item), an ordinary instance method receives cart as its first argument. The same method body can therefore inspect a different cart on each call. Static methods and class methods use different binding rules.
Worked example
Pythonclass Counter:
def __init__(self):
self.count = 0
def record(self):
self.count += 1
counter = Counter()
record = counter.record
record()
assert record.__self__ is counter
assert counter.count == 1Explore class relationships in the OOP Playground