βAapne Python me kabhi likha hoga: x = 5, name = 'DeathCode', ya nums = [1, 2, 3]...
Lekin kya aapko pata hai
5
bhi ek object hai?
int
,str
,list
,tuple
,dict
β¦ sab kuch β har ek choti se choti cheez Python me ek object hoti hai!Aaj ki video me hum samjhenge Python ke andar ka asli system β Object Model.
Tyaar ho jao, because aaj se Python sirf ek language nahi, ek soch banne wali hai.β
π [Section 1: Statement That Changes Everything]
βIn Python, everything is an object.
Haan, sab kuch. Functions bhi. Modules bhi. Classes bhi. Even keywords jaise
True
,False
,None
β sab kuch.β
Python ka mantra hai:
π§ "If it has a value, it is an object."
π§± [Section 2: Python mein Object kya hota hai?]
π§
-
Python mein Object ka matlab hai: value + type + methods
-
Har object kisi na kisi class ka instance hota hai
-
Har object ke paas 3 important cheezein hoti hain:
- Identity β memory location (use
id()
function) - Type β kis type ka object hai (
int
,str
,list
, etc.) - Value β actual data stored inside the object
- Identity β memory location (use
π Example:
x = 5
print(type(x)) # <class 'int'>
print(dir(x)) # int object ke sabhi methods dikhaata hai
Har object ek box ki tarah socho β jisme kuch data (value) hai, ek label (type) hai, aur uske sath kuch tools (methods) lage hain.
𧬠[Section 3: Python ke Basic Data Types ke Internals]
int
object:
π― 1. - Ye immutable hota hai β ek baar bana to usko change nahi kiya ja sakta.
- Python small integers ko (-5 se 256 tak) cache karta hai β matlab ek hi object baar-baar use karta hai performance ke liye.
a = 100
b = 100
print(id(a) == id(b)) # True β dono same object ko point kar rahe hain
str
object:
π― 2. str
bhi immutable hota hai.- Python internally ise ek character array ki tarah handle karta hai.
- Agar aap same string baar-baar use karo to Python usko intern karta hai β ek hi copy reuse hoti hai.
list
object:
π― 3. - Ye mutable hota hai β aap data update/change kar sakte ho.
- Ye reference based hota hai β iska matlab agar do variable same list ko point karein to change dono mein dikhega.
- Lists mein mixed data types store kiye ja sakte hain.
lst = [1, "a", True]
print(type(lst)) # <class 'list'>
tuple
object:
π― 4. - Ye
list
ka immutable version hai β secure aur fast hota hai fixed data ke liye.
tpl = (1, 2, 3)
dict
object:
π― 5. - Ye key-value pair format mein data store karta hai.
- Internally Python isme hash map use karta hai β isse fast access milta hai.
d = {"name": "DeathCode", "yt": "π₯"}
Dictionary ek mini-database ki tarah kaam karta hai β jisme aap har key se data retrieve kar sakte ho.
π§ [Section 4: id(), type(), dir() β Python ke 3 Detective Tools]
Ye 3 built-in functions Python mein kisi bhi object ko deeply samajhne ke liye use hote hain:
x = 42
print(id(x)) # Object ki memory location
print(type(x)) # Object ka type
print(dir(x)) # Available methods aur properties
In tino tools se aap Python mein kisi bhi unknown object ko investigate kar sakte ho β bilkul Sherlock Holmes ki tarah! π΅οΈββοΈ
π [Section 5: Sab kuch Class ka Instance hai]
Python mein har cheez kisi class ka object hoti hai β numbers, strings, even aapke banaye hue classes bhi:
print(isinstance(10, int)) # True
print(isinstance("Hi", str)) # True
print(isinstance([1, 2], object)) # True
Python mein sab kuch ek hierarchy follow karta hai β aur sabka base hota hai object class.
π§ [Section 6: Python ka Type System β Dynamic aur Strong]
π―
- Dynamic: Python variables ki type run-time pe decide hoti hai β aap likhte waqt specify nahi karte.
- Strong: Python automatic type conversion allow nahi karta β galat types ko mix karne pe error deta hai.
x = "5"
y = 2
print(x + y) # Error! (str + int not allowed)
Aapko types ka dhyan rakhna padta hai β warna TypeError aa sakta hai.
π [Section 7: Interpreter ke Peeche kya hota hai?]
x = 10
π Jab aap ye likhte ho:
Interpreter ye steps karta hai:
- Ek
int
object10
banata hai (ya cache se leta hai) - Variable
x
us object ko reference karta hai (bind hota hai)
Matlab x khud mein data nahi rakhta β woh sirf memory location ko point karta hai.
π₯ [Section 8: Python ke Kuch Secret Optimizations]
π‘ Python kuch values ke liye interning/caching karta hai β performance improve karne ke liye:
- Small integers: 5 to 256
- Short strings:
'yes'
,'no'
, etc.
a = 256
b = 256
print(a is b) # True β same memory location
a = 257
b = 257
print(a is b) # False β alag object bana
Is optimization ka matlab: repeated values par Python memory save karta hai by reusing objects.
π [Section 9: Real-Life Example to Explain Objects]
βSocho tumhare paas ek suitcase hai. Har suitcase me kuch saman hota hai (value), ek tag laga hota hai (identity), aur ek color ya label hota hai (type).
Har variable Python me ek suitcase ka reference hota hai.β
β‘ [Section 10: Mind-Blowing Fact]
βEven functions and classes are objects!β
def greet():
print("Hello")
print(type(greet)) # <class 'function'>
π§Ή [Practice Zone β Test Your Understanding]
π§ͺ Try these:
-
Use
id()
,type()
anddir()
on:x = 99
name = "Python"
nums = [1, 2, 3]
def test(): pass
-
Is
a is b
always true for same numbers? Try257
-
Create a function, assign it to another variable, and call using new name. What does that prove?
Keep it up
βAb jab bhi aap Python me koi value likhenge, ya koi variable banayenge, ya koi function call karengeβ¦ aap ye nahi bhoolenge ki wo ek object hai.β
βPython ke objects simple nahi, smart hote hain.
Aur aap bhi, kyunki aapne Python ke andar ghuske dekha hai uska asli structure.β
βAgli video me hum dekhenge:
Python Memory Handling β Heap, Stack, Reference Counting, and Garbage Collection!
Aisa topic jise samajhne ke baad aap galtiyan nahi karenge memory ke mamle me.β