Python Learning Series · @onlysamarrr · Day 30 / 30
Day 30 introduces OOP. A class is a blueprint and an object is a real thing made from it. __init__ is the constructor that runs when an object is created, and self is the object the method was called on.
Phase: Intermediate · Reels: 146/450 – 150/450
Day 30 introduces OOP. A class is a blueprint and an object is a real thing made from it. __init__ is the constructor that runs when an object is created, and self is the object the method was called on.
class BankAccount:
bank = "SBI"
def __init__(self, owner, balance=0):
self.owner = owner
self._balance = balance
def deposit(self, amt):
if amt <= 0:
raise ValueError("Amount must be > 0")
self._balance += amt
@property
def balance(self):
return self._balance
acc = BankAccount("Arhan")
acc.deposit(500)
print(acc.balance, acc.bank)
print(acc.owner)
500 SBI Arhan
Bank accounts, users, orders and products in real apps are all classes with objects.
Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.
Q1. What is a class?
Q2. What does __init__ do?
Q3. What happens if a method forgets self?
| 146/450 | Concept | Class = blueprint |
| 147/450 | Live demo | Pehli class banao |
| 148/450 | Common mistake | self bhoolna |
| 149/450 | Quiz | Quiz: Car class |
| 150/450 | Interview / real life | Real life: OOP kahan use hota |