Python · Chapter 2.10 · Pro level
OOP in Python: Classes & Objects (Make Your Own Game Character)
Python में OOP: Classes और Objects (अपना गेम कैरेक्टर बनाओ)
A class is a blueprint (like a cookie cutter) and an object is a real thing made from it (a cookie). class Hero: describes what every hero HAS (attributes: name, hp) and CAN DO (methods: attack, heal). __init__ is the special setup method that runs when you create an object: h = Hero("Arjun", 100). Inside methods, self means "this particular object" — so self.hp is this hero's HP. One class → 1000 different heroes!
Class एक नक्शा (blueprint) है (कुकी कटर जैसा) और object उससे बनी असली चीज़ (कुकी)। class Hero: बताता है कि हर हीरो के पास क्या है (attributes: name, hp) और वह क्या कर सकता है (methods: attack, heal)। __init__ ख़ास setup method है जो object बनते ही चलता है: h = Hero("Arjun", 100)। Methods के अंदर self का मतलब है "यही वाला object" — इसलिए self.hp यानी इस हीरो की HP। एक class → 1000 अलग हीरो!
Words and symbols in this chapter
class Hero:- Starts the blueprint named
Hero(class names start with a Capital). Heroनाम का नक्शा शुरू करता है (class का नाम कैपिटल से शुरू)।def __init__(self, name, hp):- Setup method — runs automatically on
Hero(...); two underscores on each side. - Setup method —
Hero(...)पर अपने-आप चलता है; दोनों तरफ़ दो underscore। self.name = name- Saves the value inside THIS object.
- वैल्यू को इसी object के अंदर सेव करता है।
def attack(self, enemy):- A method: an action the object can do. First parameter is always
self. - Method: object का एक काम। पहला parameter हमेशा
self। h = Hero("Arjun", 100)- Creates a real object from the blueprint.
- नक्शे से असली object बनाता है।
Ready example
class Hero:
def __init__(self, name, hp):
self.name = name
self.hp = hp
def take_hit(self, damage):
self.hp = self.hp - damage
print(f"{self.name} ko {damage} damage! HP ab {self.hp}")
def is_alive(self):
return self.hp > 0
arjun = Hero("Arjun", 100)
bheem = Hero("Bheem", 150)
arjun.take_hit(30)
bheem.take_hit(50)
print("Arjun zinda?", arjun.is_alive())
What you will see: Memory boxes show arjun and bheem as two separate objects with their own HP.
मेमोरी डिब्बों में arjun और bheem अपनी-अपनी HP वाले दो अलग objects दिखेंगे।
Line by line
class Hero:- Blueprint for every hero.
- हर हीरो का नक्शा।
def __init__(self, name, hp): ...- Stores name and hp inside each new object.
- हर नए object में name और hp रखता है।
def take_hit(self, damage): self.hp = self.hp - damage- A method that changes this hero's own HP.
- Method जो इसी हीरो की अपनी HP बदलता है।
arjun = Hero("Arjun", 100) / bheem = Hero("Bheem", 150)- Two separate objects from one class — each has its own HP.
- एक class से दो अलग objects — हर एक की अपनी HP।
Common mistake
✗ Wrong
def speak():
print(name)✓ Right
def speak(self):
print(self.name)Methods need self as the first parameter and attributes are read with self..
Methods को पहला parameter self चाहिए और attributes self. से पढ़े जाते हैं।
Your task
Complete the Pet class: save sound in __init__, add a speak(self) method, then create a pet and call speak().
Pet class पूरी करो: __init__ में sound सेव करो, speak(self) method जोड़ो, फिर एक pet बनाकर speak() चलाओ।
- In
__init__addself.sound = sound(8 spaces in front).__init__मेंself.sound = soundजोड़ो (आगे 8 स्पेस)। - Add
def speak(self):(4 spaces) withprint(self.name + " says " + self.sound)(8 spaces).def speak(self):(4 स्पेस) और अंदरprint(self.name + " says " + self.sound)(8 स्पेस) जोड़ो। - At the bottom (no spaces):
dog = Pet("Tommy", "Bhow Bhow")anddog.speak().
सबसे नीचे (बिना स्पेस):dog = Pet("Tommy", "Bhow Bhow")औरdog.speak()।
Starter code for the task
class Pet:
def __init__(self, name, sound):
self.name = name
# TODO 1: self.sound = sound likho
# TODO 2: def speak(self): banao jo print kare name + " says " + sound
# TODO 3: dog = Pet("Tommy", "Bhow Bhow") banao aur dog.speak() chalaoDo this task in the classroom: your code is checked automatically and you get hints if something is missing.
Recap
- Class = blueprint, object = real thing made with
ClassName(...). __init__(self, ...)sets up attributes likeself.name.- Every method gets
selffirst — it means "this object".
- Class = नक्शा, object =
ClassName(...)से बनी असली चीज़। __init__(self, ...)self.nameजैसे attributes सेट करता है।- हर method में पहले
selfआता है — मतलब "यही object"।
Quick quiz
What does self mean inside a method?
Method के अंदर self का क्या मतलब है?
Show the answer
Answer: B (The current object the method is working on)
self refers to the object that called the method, e.g. arjun in arjun.take_hit(30).
self उस object को दर्शाता है जिसने method बुलाया, जैसे arjun.take_hit(30) में arjun।
Practice problems for this chapter
- Fix the UPI Wallet Class
UPI Wallet Class ठीक करो
Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.