κ°λ
The location where we can find a variable and also access it if required is called the scope of a variable.
λ³μλ₯Ό μ°Ύκ³ νμν κ²½μ° λ³μμ μ‘μΈμ€ν μ μλ μμΉλ₯Ό λ³μμ μ ν¨ λ²μλΌκ³ νλ€.
LEGB Rules / LEGB κ·μΉ
Python resolves names using the so-called LEGB rule, which is named after the Python scope for names. The letters in LEGB stand for Local, Enclosing, Global, and Built-in.
LEGB κ·μΉμ΄λ Pythonμμ λ³μλ₯Ό μ°Έμ‘°ν λ Local(μ§μ μ€μ½ν) → Enclosing(λ΄ν¬ ν¨μ μ€μ½ν) → Global(μ μ μ€μ½ν) → Built-in(λ΄μ₯ μ€μ½ν) μμλ‘ λ³μλ₯Ό νμνλ μμΉμ μλ―Ένλ€.
π Local
- ν¨μ λ΄λΆμμ μ μλ λ³μ
- ν¨μ λ΄λΆμμλ§ μ κ·Ό κ°λ₯νλ©° ν¨μ νΈμΆμ΄ λλλ©΄ λ©λͺ¨λ¦¬μμ μ κ±°λ¨
- ν¨μ μΈλΆμμ μ§μλ³μμ μ κ·Όνλ €κ³ ν κ²½μ° NameError: name λ³μλͺ is not defined λ°μ
- μ½λ μμ
def f():
# local variable
s = "I love Geeksforgeeks"
print(s)
# Driver code
f()
# Output: I love Geeksforgeeks
π Enclosing
1) κ°λ
- μ€μ²© ν¨μμλ§ μ‘΄μ¬νλ νΉμν λ²μ
- ν¨μ λ΄λΆμ μ€μ²©λ ν¨μκ° μμ λ μ€μ²©λ ν¨μμ λ°κΉ₯μͺ½ ν¨μ(λ΄ν¬ ν¨μ)μ λ³μ μμμ μ°Έμ‘°νλ κ²
- λ°κΉ₯μͺ½ ν¨μμμ μ μλ λ³μλ nonlocal λ³μλ‘ κ°μ£Ό
- λ΄ν¬λ ν¨μλ λ°κΉ₯ ν¨μμ λ³μλ₯Ό μ½μ μλ μμ§λ§ κ°μ λ³κ²½νλ €λ©΄ nonlocal ν€μλμ λͺ μμ μΈ μ¬μ© νμ → Pythonμ΄ μ§μ λ³μμ λΉμ§μ λ³μλ₯Ό ꡬλΆνκΈ° λλ¬Έ
2) νμ κ·μΉ
- λ΄λΆ ν¨μλ μμ μ local μ€μ½ν λ¨Όμ νμ
- Local μ€μ½νμ λ³μκ° μμΌλ©΄ λ°λ‘ λ°κΉ₯μͺ½ ν¨μ(enclosing)μ μ€μ½νμμ λ³μ νμ
- Enclosing μ€μ½νμμ λ³μλ₯Ό μ°Ύμ μ μμΌλ©΄, λ€μ λ¨κ³λ‘ globalκ³Ό built-in μ€μ½ν νμ
3) Enclosing μ€μ½νμ λ³μ μ½κΈ°
- inner_functionμμ νΉμ λ³μλ₯Ό μ°Έμ‘°νλ €κ³ ν λ local μ€μ½ν(inner_fuction λ΄λΆ)μ μ°Έμ‘°ν λ³μκ° μλ€λ©΄ enclosing μ€μ½ν(λ°κΉ₯μͺ½ ν¨μ)μμ ν΄λΉ λ³μλ₯Ό μ°Ύμ
- μ½λ μμ
def outer_function():
x = "enclosing variable" # Enclosing λ²μμ μλ λ³μ
def inner_function():
print(x) # Enclosing μ€μ½νμ λ³μ μ°Έμ‘°
inner_function()
outer_function()
# Output: enclosing variable
4) Enclosing μ€μ½νμ λ³μ μμ
- Enclosing μ€μ½νμ λ³μλ₯Ό μμ νλ €λ©΄ nonlocal ν€μλλ₯Ό μ¬μ©ν΄μΌ ν¨
- Nonlocal ν€μλλ₯Ό μ¬μ©νμ§ μμ κ²½μ° λ΄λΆ ν¨μμ local μ€μ½νμ μλ‘μ΄ λ³μκ° μμ±λλ―λ‘ enclosing μ€μ½νμ λ³μλ λ³κ²½λμ§ μμ
- μ½λ μμ
def outer_function():
x = "enclosing variable" # Enclosing λ²μμ μλ λ³μ
def inner_function():
nonlocal x # Enclosing λ²μμ λ³μ μ¬μ© μ μΈ
x = "modified by inner_function" # Enclosing λ³μ μμ
inner_function()
print(x) # μμ λ κ° μΆλ ₯
outer_function()
# Output: modified by inner_function
π Global
- ν¨μ μΈλΆμμ μ μλκ³ μ μΈλλ©° μ΄λ€ ν¨μμλ μ§μ λμ§ μμ λ³μ
- νλ‘κ·Έλ¨μ μ΄λ λΆλΆμμλ μ¬μ© κ°λ₯νμ§λ§ ν¨μ λ΄λΆμμ κ°μ μμ νλ €λ©΄ global ν€μλλ₯Ό μ¬μ©ν΄μΌ ν¨
- 무λΆλ³ν μ μλ³μ μ¬μ©μ μ½λμ κ°λ μ±μ λ¨μ΄λ¨λ¦¬κ³ μ μ§λ³΄μλ₯Ό μ΄λ ΅κ² ν μ μμ
- μ½λ μμ
x = 20 # μ μ λ³μ
def modify_global():
global x
x = 30 # μ μ λ³μ μμ
modify_global()
print(x)
# Output: 30
π Built-in
- Pythonμ λ΄μ₯ λ€μμ€νμ΄μ€μμ λ³μ(len, print, ...) νμ
μ§μλ³μμ μ μλ³μ μΆ©λ
Now suppose a variable with the same name is defined inside the scope of the function as well then it will print the value given inside the function only and not the global value.
μ§μλ³μμ μ μλ³μμ μ΄λ¦μ΄ κ°μ κ²½μ° Pythonμ μ§μλ³μλ₯Ό μ°μ μ μΌλ‘ μ¬μ©νλ€.
μ°Έκ³
Python Scope of Variables - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
Python Scope & the LEGB Rule: Resolving Names in Your Code – Real Python
In this step-by-step tutorial, you'll learn what scopes are, how they work, and how to use them effectively to avoid name collisions in your code. Additionally, you'll learn how to take advantage of a Python scope to write more maintainable and less buggy
realpython.com