πŸ‘¨‍🏫

[Python] Scope of variables / λ³€μˆ˜μ˜ 유효 λ²”μœ„

geum 2024. 11. 19. 18:08

κ°œλ…

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