Instance Methods and self
15 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)An instance method is a function inside a class that works on one object. It is the kind of method you write most often. Class methods and static methods exist too, but they are for rarer jobs.
An instance method works on one object
Instance method is a method that receives the object as self. Use it when the work reads or changes that one object's data.
A method that needs an object's data takes self as its first parameter. Python fills self with the object before the dot.
class Account: def __init__(self, owner, balance): self.owner = owner self.balance = balance
def deposit(self, amount): self.balance += amount
acct = Account("Ira", 1000)acct.deposit(500)print(acct.balance)Example 1 · An instance method changes its own object
class Account: def __init__(self, owner, balance): self.owner = owner self.balance = balance
def deposit(self, amount): self.balance += amount return self.balance
acct = Account("Ira", 1000)print(acct.deposit(500))print(acct.balance)1500 1500
What this code does
depositusesself.balance, so it only makes sense for one account.- The returned value and the stored balance match.
In acct.deposit(500), self is acct and amount is 500. The change lands on this account only.
Change the object or return a value
A method can change the object, return a value, or do both. A method with no return gives back None.
Example 2 · Read-only and changing methods
class Product: def __init__(self, name, price): self.name = name self.price = price
def price_with_tax(self, rate): return self.price + self.price * rate // 100
def apply_discount(self, percent): self.price -= self.price * percent // 100
p = Product("Pen", 200)print(p.price_with_tax(18))print(p.price)p.apply_discount(10)print(p.price)236 200 180
What this code does
price_with_taxonly reads the price, so it stays 200 after the call.apply_discountassigns toself.price, so the object itself changes to 180.
Example 3 · A method that forgets to return
class Cart: def __init__(self, price, qty): self.price = price self.qty = qty
def total(self): self.price * self.qty
def total_fixed(self): return self.price * self.qty
cart = Cart(250, 3)print(cart.total())print(cart.total_fixed())None 750
What this code does
totalworks out 750 and then throws it away, so the call givesNone.total_fixedaddsreturn, and the caller receives 750.
Example 4 · Changing the object or returning a value
class Account: def __init__(self, owner, balance): self.owner = owner self.balance = balance
def withdraw(self, amount): self.balance -= amount
def balance_after_fee(self, fee): return self.balance - fee
acct = Account("Ira", 1000)print(acct.balance_after_fee(25))print(acct.balance)print(acct.withdraw(200))print(acct.balance)975 1000 None 800
What this code does
balance_after_feeonly reads, so the balance stays 1000.withdrawchanges the balance and returns nothing, so printing its result showsNone.
Methods call other methods
Bound method is an instance method looked up on an object, such as acct.deposit. It remembers the object and passes it as self when called.
Inside a method, self.other_method() calls another method on the same object. Small methods built on each other are easier to test than one long method.
Example 5 · Two objects, one method
class Account: def __init__(self, owner, balance): self.owner = owner self.balance = balance
def deposit(self, amount): self.balance += amount
def withdraw(self, amount): if amount > self.balance: return False self.balance -= amount return True
def transfer_to(self, other, amount): if self.withdraw(amount): other.deposit(amount) return True return False
a = Account("Ira", 300)b = Account("Omar", 50)print(a.transfer_to(b, 200))print(a.transfer_to(b, 200))print(a.balance, b.balance)True False 100 250
What this code does
- The first transfer works: 200 leaves
aand arrives inb. - The second fails because
ahas only 100 left, so nothing moves andFalseis returned.
Example 6 · The object is the first argument
class Counter: def __init__(self): self.count = 0
def add(self): self.count += 1
c = Counter()c.add()Counter.add(c)print(c.count)add_one = c.addadd_one()print(c.count)2 3
What this code does
c.add()andCounter.add(c)do the same thing: the object becomesself.c.addwithout brackets is a bound method. It remembersc, soadd_one()still changesc.
Example 7 · One method calling others
class Account: def __init__(self, owner, balance): self.owner = owner self.balance = balance
def deposit(self, amount): self.balance += amount
def withdraw(self, amount): if amount > self.balance: return False self.balance -= amount return True
def transfer_to(self, other, amount): if self.withdraw(amount): other.deposit(amount) return True return False
a = Account("Ira", 500)b = Account("Omar", 100)print(a.transfer_to(b, 200))print(a.balance, b.balance)True 300 300
What this code does
selfis the sender andotheris a second object of the same class.transfer_toreuseswithdrawanddepositinstead of repeating their rules.
Two other kinds of method
A class method (@classmethod) receives the class as cls and is often used to build objects from another input, such as a text line. A static method (@staticmethod) receives neither self nor cls; it is a plain function kept inside the class. The Class and Static Methods module (opens in a new tab) covers both in detail.
Next step
Check your understanding
10 quick questions