Everything in Python is an Object πŸ’‘ Internals Explained #18 || Python ka Pitara || DeathCode - DeathCode

β€œ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

πŸ“Œ 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]

🎯 1. int object:

  • 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

🎯 2. str object:

  • 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.

🎯 3. list object:

  • 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'>

🎯 4. tuple object:

  • Ye list ka immutable version hai – secure aur fast hota hai fixed data ke liye.
tpl = (1, 2, 3)

🎯 5. dict object:

  • 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:

  1. Ek int object 10 banata hai (ya cache se leta hai)
  2. 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:

  1. Use id(), type() and dir() on:

    • x = 99
    • name = "Python"
    • nums = [1, 2, 3]
    • def test(): pass
  2. Is a is b always true for same numbers? Try 257

  3. 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.”

Β© 2024 DeathCode. All Rights Reserved.