Python Series โ Part 8: Scope & Execution Context (LEGB Rule) Full Guide
๐ Introduction
"Kabhi aisa laga hai ki ek variable function ke bahar to kaam karta hai, lekin function ke andar jaake usko bhool jaata hai Python?
Kabhi kisi variable ko access karne ki koshish ki, aur Python bola โ NameError?
Yaar... ye sab hota kyu hai?
๐ค Answer hai โ Scope aur Execution Context.
Aur aaj hum ghusenge Python ke LEGB Rule ke andar โ jaise ghuste hain kisi detective case me! ๐ฎโโ๏ธ"
๐ Section 1: What is Scope in Python?
"Scope ka matlab hai โ kahaan tak koi variable zinda hai ya accessible hai.
Har variable ka ek area hota hai โ uske bahar use nahi kar sakte."
Real-Life Analogy:
"Socho tumhara college ID sirf college ke andar valid hai. Bahar jaake wo valid nahi hai. Waise hi variable ka bhi apna ek valid area hota hai."
๐๏ธ Section 2: Execution Context โ Function Execution Behind the Scenes
"Jab Python koi function execute karta hai, to wo ek execution context banata hai โ jisme variables track hote hain."
Think of it like:
Main Room (Global Scope)
โโโ Function Room 1 (Local Scope)
โ โโโ Inner Function Room (Enclosed Scope)
"Har function call ke sath ek naya frame banta hai jisme apne variables aur values hoti hain."
๐ Section 3: LEGB Rule โ Pythonโs Variable Lookup Chain
"Jab Python kisi variable ko access karta hai, wo ye order follow karta hai:"
๐ LEGB = Local โ Enclosing โ Global โ Built-in
๐น Local Scope (L)
def func():
x = 10
print(x)
"x function ke andar define hai โ Local Scope."
๐น Enclosing Scope (E)
def outer():
x = "Outer Value"
def inner():
print(x)
inner()
"inner() ke andar x nahi mila, to outer() ke x ko use karega."
๐น Global Scope (G)
x = "Global"
def func():
print(x)
func()
"Function ke andar nahi mila, to global scope ka x use hua."
๐น Built-in Scope (B)
print(len("DeathCode"))
"Python apne built-in functions me check karta hai agar variable ya function kahin aur nahi milta."
๐ฅ Section 4: Name Shadowing โ Variable Conflict
x = "global"
def func():
x = "local"
print(x)
func()
print(x)
"Function ke andar x redefine ho gaya โ Local variable ne Global variable ko shadow kar diya."
๐ Section 5: Hoisting in Python?
"JavaScript me hoisting hoti hai โ Python me nahi."
Example:
print(x)
x = 10
Output: NameError: name 'x' is not defined
"Python me variables ko declare karne se pehle use nahi kar sakte."
this
in Python vs JavaScript
๐ Section 6: "JavaScript me
this
hota hai โ Python meself
hota hai."
JS Example:
const obj = {
name: "DeathCode",
print: function () {
console.log(this.name);
}
};
Python Equivalent:
class YouTube:
def __init__(self, name):
self.name = name
def print(self):
print(self.name)
"Python ka self object-oriented programming me kaam karta hai."
๐ฅ Section 7: Anonymous Functions (Lambda Functions)
"Lambda = Short Anonymous Function."
Example:
add = lambda a, b: a + b
print(add(5, 3))
Feature | Normal Function | Lambda Function |
---|---|---|
Named? | Yes | No |
Multi-line? | Yes | No |
Syntax | def |
lambda |
Return | Explicit | Implicit |
Use-case | General | Short functions |
"Lambda me complex logic nahi likhte โ sirf chhoti expressions."
โ Section 8: Function Use Before Declaration?
Example:
say_hello()
def say_hello():
print("Hello")
Output: NameError
"Python me line-by-line execution hota hai. Pehle define karo, fir call karo."
๐ Section 9: Summary (Quick Recap)
- Scope = Variable ka accessible area
- LEGB = Local > Enclosing > Global > Built-in
- No Hoisting in Python
- Python uses
self
, notthis
- Lambda = Anonymous one-liner functions
๐งโ๐ Outro
"Aaj humne Python ke Scope, Execution Context aur LEGB ka gyaan le liya! ๐ก
Agla video aur bhi interesting hoga โ Python Execution Model โ kaise Python aapka code execute karta hai behind the scenes.
Channel ko subscribe karo, aur agar doubts hain to comment section me puchna mat bhoolna! ๐ฅ"