Scope in Python
Python mein Scope decide karta hai ki koi variable program ke kis part mein accessible hai. Simple words mein, scope batata hai ki variable ko kahan use kar sakte hain.
1. What is Scope?
Scope ka matlab hai variable ki visibility aur accessibility.
Python mein mainly do important scopes beginners ko samajhna chahiye:
- Local Scope
- Global Scope
2. Local Scope
Function ke andar create kiya gaya variable generally local variable hota hai. Usse function ke andar access kiya ja sakta hai.
def greet():
message = "Hello CIIT"
print(message)
greet()
Output:
Hello CIIT
Yahan message function ke andar create hua hai, isliye iska scope function ke andar hai.
3. Local Variable Outside Function
Local variable ko function ke bahar directly access karne ki koshish karne par error aayega.
def greet():
message = "Hello CIIT"
print(message)
greet()
print(message)
message function ka local variable hai.
Isliye function ke bahar directly accessible nahi hai.
4. Global Scope
Function ke bahar create kiya gaya variable global variable hota hai.
course = "Python Full Stack"
def show_course():
print(course)
show_course()
print(course)
Output:
Python Full Stack
Python Full Stack
Global variable function ke andar bhi access kiya ja sakta hai.
5. Local Scope vs Global Scope
| Local Variable | Global Variable |
|---|---|
| Function ke andar create hota hai | Function ke bahar create hota hai |
| Function ke andar accessible hota hai | Program ke multiple parts mein accessible ho sakta hai |
| Limited scope | Wider scope |
6. Same Variable Name in Different Scopes
Local aur global scope mein same variable name ho sakta hai.
course = "Python"
def show_course():
course = "Java"
print("Inside:", course)
show_course()
print("Outside:", course)
Output:
Inside: Java
Outside: Python
Function ke andar local course use hua, jabki function ke bahar global course use hua.
7. global Keyword
Agar function ke andar global variable ko modify karna ho, toh global keyword use kar sakte hain.
count = 10
def update_count():
global count
count = 20
update_count()
print(count)
Output:
20
8. Why global is Needed?
Function ke andar assignment karne par Python normally us variable ko local variable samajhta hai.
count = 10
def update_count():
count = 20
print("Inside:", count)
update_count()
print("Outside:", count)
Output:
Inside: 20
Outside: 10
Yahan function ke andar naya local variable create hua. Global variable change nahi hua.
9. Global Variable Example 🤓❤️
institute = "CIIT Training Institute 🤓👀...!"
def display_institute():
print(institute)
display_institute()
Output:
CIIT Training Institute 🤓👀...!
10. Function Parameters and Scope
Function parameters bhi function ke andar local variables ki tarah behave karte hain.
def greet(name):
print("Hello", name)
greet("Amit")
Yahan name parameter hai aur function ke local scope mein available hai.
11. Nested Function Scope
Python mein ek function ke andar doosra function define kiya ja sakta hai.
def outer():
message = "Hello from Outer"
def inner():
print(message)
inner()
outer()
Output:
Hello from Outer
Inner function outer function ke variable ko access kar sakta hai.
12. Enclosing Scope
Nested functions mein outer function ka scope enclosing scope kehlata hai.
def outer():
name = "CIIT"
def inner():
print(name)
inner()
outer()
Output:
CIIT
13. LEGB Rule
Python variable ko search karte waqt LEGB Rule follow karta hai.
| Letter | Scope |
|---|---|
| L | Local |
| E | Enclosing |
| G | Global |
| B | Built-in |
Local → Enclosing → Global → Built-in
14. Built-in Scope
Python ke built-in functions aur objects built-in scope mein available hote hain.
numbers = [10, 20, 30]
print(len(numbers))
print(max(numbers))
print(sum(numbers))
Output:
3
30
60
15. CIIT Example 👀🤓
Suppose CIIT application mein institute name global hai, aur student name function ke andar local hai.
institute = "CIIT Training Institute ❤️😍...!"
def student_details(name):
course = "Python Full Stack"
print("Institute:", institute)
print("Student:", name)
print("Course:", course)
student_details("Rahul")
Output:
Institute: CIIT Training Institute ❤️😍...!
Student: Rahul
Course: Python Full Stack
16. Scope Best Practices
- Variables ko required scope tak hi limit rakho.
- Unnecessary global variables avoid karo.
- Function inputs ke liye parameters use karo.
- Function se result lene ke liye return use karo.
- Meaningful variable names use karo.
17. Common Mistakes
def student():
name = "Amit"
student()
print(name)
Yahan name local variable hai. Isliye function ke bahar access nahi kiya ja sakta.
18. Interview Points
- Python mein scope kya hota hai?
- Local aur global variable mein kya difference hai?
- global keyword ka use kab hota hai?
- LEGB rule kya hai?
- Function parameter ka scope kya hota hai?
- Kya inner function outer function ke variable ko access kar sakta hai?
CIIT Learning Point
Scope samajhne se aapko clear hota hai ki Python mein variables ko kahan create, access aur modify kiya ja sakta hai. Local, Enclosing, Global aur Built-in scopes ko LEGB Rule ke through understand karna Python programming ke liye important hai.
CIIT Practice Tasks
-
Local Variable:
Ek function create karo jisme student kanamelocal variable ho aur usse print karo. -
Global Variable:
institute = "CIIT Training Institute"global variable create karo aur function ke andar display karo. -
Same Variable Name:
Global aur local scope mein same variable name use karke dono values display karo. -
global Keyword:
Ek global counter variable create karo aur function ke andar global keyword use karke uski value update karo. -
Nested Function:
Outer function mein student name create karo aur inner function se usse display karo. -
LEGB Practice:
Local, enclosing aur global variables ka example create karo aur identify karo Python kis value ko pehle search karta hai.
Summary :
Python mein scope variable ki visibility aur accessibility decide karta hai. Function ke andar banaya gaya variable generally local scope mein hota hai, jabki function ke bahar banaya gaya variable global scope mein hota hai. Nested functions mein enclosing scope bhi important hota hai. Python variables ko LEGB order mein search karta hai: Local, Enclosing, Global aur Built-in.