Python Scope System ๐Ÿงฌ | LEGB Rule & Function Execution Flow #8 || Python ka Pitara || DeathCode - DeathCode

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


๐Ÿ”ž Section 6: this in Python vs JavaScript

"JavaScript me this hota hai โ€” Python me self 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, not this
  • 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! ๐Ÿ”ฅ"

ยฉ 2024 DeathCode. All Rights Reserved.