Python-ல் ஒரு variable எங்கே create செய்யப்படுகிறது, அந்த variable-ஐ எந்த இடத்திலிருந்து access செய்யலாம் என்பதைத்தான் Scope என்று சொல்கிறோம்.
இந்த concept-ல் முதலில் இரண்டு முக்கிய scope-கள்:
Scope என்றால் என்ன? Scope is the region of a program where a variable can be accessed or used. தமிழில்: ஒரு variable-ஐ program-ன் எந்த பகுதியில் access செய்யலாம் என்பதை தீர்மானிக்கும் boundary தான் Scope.
Python-ல் ஒரு variable எங்கே create செய்யப்படுகிறது, அந்த variable-ஐ எந்த இடத்திலிருந்து access செய்யலாம் என்பதைத்தான் Scope என்று சொல்கிறோம்.
இந்த concept-ல் முதலில் இரண்டு முக்கிய scope-கள்:
Simple-ஆ:
Variable எங்கே visible? எங்கே usable? என்பதுதான் Scope.
Scope என்றால் என்ன?
Scope is the region of a program where a variable can be accessed or used.
ஒரு variable-ஐ program-ன் எந்த பகுதியில் access செய்யலாம் என்பதை தீர்மானிக்கும் boundary தான் Scope.
1x = 10
இந்த x எங்கே create செய்யப்பட்டது?
Function வெளியே.
Global Variable
ஒரு college example எடுத்துக்கொள்ளலாம்.
Tomorrow Holiday
அதை எல்லா departments-மும் பார்க்கலாம்.
Global
என்று நினைத்துக்கொள்ளலாம்.
ஆனால் Computer Science department staff room-ல் மட்டும் இருக்கும் ஒரு note:
CS Meeting at 2 PM
அதை அந்த room / department-க்குள் மட்டும் use செய்வோம்.
Local
என்று நினைத்துக்கொள்ளலாம்.
Global = பெரிய area
Local = குறிப்பிட்ட area
│
├── Global Area
│ │
│ └── Global Variables
│
└── Function
│
└── Local Area
│
└── Local Variables1x = 100
1def test(): 2 y = 20
Local Scope என்றால் என்ன?
ஒரு variable function உள்ளே create செய்யப்பட்டால், அது பொதுவாக அந்த function-க்கு local variable.
1def show(): 2 x = 10 3 print(x)
10
show()
10
1x = 10
NameError
function உள்ளே create செய்யப்பட்டது.
So:
x → Local Variable
இந்த x-ஐ function வெளியே access செய்ய முடியுமா?
x = 10
show()
print(x)
NameError
ஏன்?
Because:
x exists only inside show() local scope.
1def student(): 2 mark = 90 3 print(mark)
90
student()
90
mark = 90
student() என்ற function define செய்கிறோம்.
mark function உள்ளே create ஆகிறது.
mark → Local Variable
print(mark)
Same function-க்குள் இருப்பதால் access செய்ய முடிகிறது.
mark = 90 student() print(mark)
Wrong.
ஏனெனில் mark local.
print(mark)
access செய்ய முடியாது.
Global Scope என்றால் என்ன?
Function-க்கு வெளியே, module/program level-ல் create செய்யப்பட்ட variable Global Variable.
1x = 100
1def show(): 2 print(x)
100
show()
100
x = 100
function வெளியே create செய்யப்பட்டது.
x → Global Variable
Function உள்ளே x என்ற local variable இல்லையென்றால் Python outer/global scope-ல் x இருக்கிறதா என்று பார்க்கும்.
x = 100
அதனால் print செய்கிறது.
name = "Python"
1def display(): 2 print(name)
Python
display()
Python
Global variable-ஐ function உள்ளே read செய்ய normally global keyword தேவையில்லை.
1x = 10
1def show(): 2 print(x)
100 50
show()
Perfectly valid.
x = 100
def show():
y = 50
print(x)
print(y)show()
100 50
works.
print(x)
print(y)
doesn't work.
?
x = Global y = Local
| Local Variable | Global Variable |
|---|---|
| Function உள்ளே create | Function வெளியே create |
| Function-க்குள் access | பல functions access செய்யலாம் |
| Limited scope | Wider scope |
| Function-specific | Program/module-level |
| Outside direct access இல்லை | Function உள்ளே read செய்யலாம் |
Same variable name Local + Global
இதுதான் exam/interview-ல் மிகவும் முக்கியம்.
x = 100
x = 20
print(x)
show()
print(x)20 100
Confusing-ஆ இருக்கலாம்.
Inch by inch பார்க்கலாம்.
| Step | x | Action |
|---|---|---|
| 01First | 100 | — |
| 02Global variable | — | Global x = 100 |
| 03def show() | 20 | Function உள்ளே x create செய்யப்பட்டது. |
| 04So | — | Local x = 20 |
| 05Inside | — | print(x) → Python local variable first check செய்கிறது. |
| 06Found | 20 | — |
| 07So output | — | 20 → Function முடிந்தது. |
| 08Outside | — | print(x) → இப்போது local scope இல்லை. |
| 09Global x | — | 100 |
| 10So output | — | 100 |
Global x
மற்றும்
Local x
இரண்டு different variables போல behave செய்யலாம்.
Global x = 100
Local x = 20
Local variable global variable-ஐ temporarily shadow செய்கிறது.
Variable Shadowing
என்று சொல்லலாம்.
Local first → Global next
Simple concept.
LEGB
அதையும் கீழே பார்ப்போம்.
இப்போது மிகவும் முக்கியமான concept.
x = 10
Function உள்ளே
x = 20
இது global x-ஐ modify செய்கிறதா?
No.
x = 10
x = 20
change()
print(x)
10
x = 20
என்று assignment செய்ததால் Python அதை new local variable ஆக எடுத்துக்கொள்கிறது.
Global x = 10
Local x = 20
Global unchanged.
keyword use செய்ய வேண்டும்.
global
global variable_name
1x = 10
1def change(): 2 global x 3 x = 20
change()
1print(x)
20
20
x = 10
Global x.
Function starts.
global x
"இந்த function-க்குள் வரும் x புதிய local variable இல்லை; வெளியே இருக்கும் global x-ஐ தான் நான் பயன்படுத்தப் போகிறேன்."
என்று சொல்கிறோம்.
x = 20
10 → 20
x = 10
print(x)
global தேவையில்லை.
x = 10
global x
x = 20
global தேவை.
Read Global → normally no global needed
Reassign Global → global needed
x = 10
print(x) x = 20 test() இது பல beginners-க்கு confusion.
UnboundLocalError
x = 20
assignment இருப்பதால் Python x-ஐ function-local variable என்று decide செய்கிறது.
print(x)
execute ஆகும்போது local x இன்னும் value பெறவில்லை.
Local x exists conceptually
but value assigned later
அதனால்:
UnboundLocalError
x = 10
def test():
global x
print(x)
x = 20test()
print(x)
10 20
Previous topic-க்கு connection மிகவும் முக்கியம்.
result = a + b
return result
a
b
எல்லாமே function local scope-ல் இருக்கின்றன.
Function parameters generally behave as local names.
1def show(name): 2 message = "Hello " + name 3 print(message)
show("Ravi")
Outside:
1print(name)
10 20
normally invalid.
ஒவ்வொரு function-க்கும் அதன் own local scope இருக்கும்.
def first():
x = 10
print(x)def second():
x = 20
print(x)first() second()
10 20
x
same name இருந்தாலும் conflict இல்லை.
first() local x = 10
second() local x = 20
Different local scopes.
first() │ └── Local x = 10 second() │ └── Local x = 20
Local scope மற்றும் lifetime related ஆனால் exactly same இல்லை.
1def test(): 2 x = 100 3 print(x)
Function execute ஆகும்போது local x available.
Function call முடிந்ததும் அந்த local name-ஐ outside function direct-ஆக access செய்ய முடியாது.
Global variable module/program-level name.
1x = 100
Program/module scope-ல் available இருக்கும்.
பல functions அதை read செய்யலாம்.
எல்லா variable-யும் global ஆக வைத்துவிடலாமா?
Technically many cases-ல் முடியும்.
தேவையில்லாமல் global variables அதிகமாக பயன்படுத்த வேண்டாம்.
Why?
எந்த function modify செய்தது கண்டுபிடிக்க difficult
Debugging hard
Side effects அதிகம்
Program maintain செய்ய difficult
Large applications-ல் confusion
Function parameters
+
Return values
+
Local variables
when possible.
result = 0
global result
result = 10 + 20
Better
return a + b
result = add(10, 20)
இந்த design cleaner.
Local variable-ஐ outside use செய்ய direct scope change செய்ய வேண்டிய அவசியமில்லை.
return மூலம் value வெளியே அனுப்பலாம்.
1def calculate(): 2 result = 100 3 return result
1x = calculate()
1print(x)
100
100
result → local
100
x → global name
Parameters → data function-க்கு உள்ளே
Return → data function-லிருந்து வெளியே
Scope → variables எங்கே usable?
Arguments
↓
Parameters (Local)
↓
Local Processing
↓
Return Value
↓
Outside variable
என்று சொல்கிறோம்.
LEGB Rule
x = Local.
def show():
x = 10Nested function-ல் outer function scope.
1def outer(): 2 x = 10
1def inner(): 2 print(x)
10
inner()
outer()
10
inner()-க்கு x local இல்லை.
Outer/enclosing scope-ல் கிடைக்கிறது.
x = 50
print(x)
Global x.
etc.
Look Everywhere, Get Best என்று memory வைத்துக்கொள்ளலாம்; ஆனால் actual order L-E-G-B.
Local + global scope படிக்கும் போது nonlocal என்ற keyword-யும் தெரிந்திருக்க வேண்டும்.
x = 10
nonlocal x x = 20 inner() print(x) outer()
20
nonlocal என்பது global-க்கு அல்ல.
Nearest enclosing function scope-ல் இருக்கும் variable-ஐ reassign செய்ய.
இதைக் கொஞ்சம் carefully புரிந்து கொள்ளுங்கள்.
numbers = [1, 2, 3]
numbers.append(4)
change()
print(numbers)
[1, 2, 3, 4]
இங்கே global numbers எழுதவில்லை.
Why?
numbers = ...
என்று name-ஐ reassign செய்யவில்லை.
append()
மூலம் modify செய்கிறோம்.
Mutation
numbers.append(4)
Existing object change.
Often global keyword தேவையில்லை.
Reassignment
numbers = [10, 20]
global numbers
தேவை.
இது interview-level important concept.
| Feature | Local | Global |
|---|---|---|
| Defined | Function உள்ளே | Function வெளியே |
| Accessible | அந்த local scope | Wider module scope |
| Parameters | Usually local | No |
| Function outside access | Normally no | Yes |
| Same name possible | Yes | Yes |
| Reassign global from function | - | global keyword |
| global vs return | Suppose function result வெளியே வேண்டும். |
| Using global: | result = 0 |
| def calculate(): | global result |
| result = 50 | Using return: |
| def calculate(): | return 50 |
| result = calculate() | Second approach often cleaner. |
Scope is about visibility.
எங்கே access செய்யலாம்?
Lifetime is about existence.
எவ்வளவு நேரம் object/name relevant?
Exam questions-ல் இரண்டையும் confuse செய்யக்கூடாது.
Local variable outside access
x = 10 print(x) Wrong.
Thinking global cannot be used inside function
Wrong.
x = 10
def test():
print(x)works.
x = 20
No.
That normally creates local x.
x = x + 1
Python considers x local because assignment exists.
But tries to read local x before assigned.
UnboundLocalError
x = 10
global x x = x + 1 test() print(x)
11
Overusing global variables
Program work செய்யலாம்.
ஆனால் maintainability குறையும்.
return x + 1 x = 10 x = increment(x)
Local
Local = Limited
Both start with L.
Local → Limited area
Global
Global = General / wider
Global → Program-level wider access
Global Keyword
READ → global keyword usually வேண்டாம்
REASSIGN → global keyword வேண்டும்
1. Function உள்ளே defined variable?
Local variable
1. Function வெளியே defined variable?
Global variable
1. Global variable-ஐ function உள்ளே read செய்ய முடியுமா?
Yes
1. Global variable-ஐ function உள்ளே reassign செய்ய keyword?
global
1. Function parameter scope?
Usually local scope.
1. Same local/global variable name இருந்தால் function உள்ளே priority?
Local
1. Python variable lookup rule?
LEGB
1. LEGB full form?
Local
Enclosing
Global
Built-in
1. Enclosing variable modify செய்ய keyword?
nonlocal
1. global மற்றும் nonlocal same?
No
Question: What is scope in Python?
Scope defines the region in which a name or variable can be accessed.
Question: What is a local variable?
A variable defined inside a function is generally local to that function.
Question: What is a global variable?
A variable defined at module level, outside functions, belongs to global scope.
Question: Can a function access a global variable?
Yes, for reading it normally can.
1x = 10
1def show(): 2 print(x) 3Question: When is global keyword required?
10
When you intend to assign/rebind a global variable from inside a function.
What is variable shadowing?
When a local variable uses the same name as a global variable, the local name hides/shadows the global name within that local scope.
What is LEGB?
Identify Scope
x = 100
def show():
y = 50
print(x, y)Answer:
Predict Output
x = 5
def test():
x = 10
print(x)
test()
print(x)Answer:
10
5
Global Modification
x = 5
def test():
global x
x = 10
test()
print(x) 10
Two Local Scopes
def a():
x = 10
print(x)
def b():
x = 20
print(x)
a()
b() 10 20
Return Local Value
def calculate():
x = 25
return x
result = calculate() print(result)
25
x local தான்.
ஆனால் its value return மூலம் outside கிடைத்தது.
Scope என்பது ஒரு variable எங்கே accessible என்பதை தீர்மானிக்கும் concept. Function-க்குள் உருவாக்கப்படும் variable பொதுவாக Local Variable; function-க்கு வெளியே module level-ல் உருவாக்கப்படும் variable Global Variable. Global variable-ஐ function உள்ளே read செய்யலாம்; ஆனால் அதை reassign செய்ய global keyword தேவைப்படும். Python variable lookup LEGB - Local, Enclosing, Global, Built-in - என்ற order-ஐ பின்பற்றுகிறது.