Search

[python] 파이썬 λ°μ½”λ ˆμ΄ν„°(decorator)

νƒ€μž…
μŠ€ν„°λ””
νƒœκ·Έ
python
μƒνƒœ
Published
생성일
2023/07/13 06:25
μ΅œμ’… νŽΈμ§‘ μΌμ‹œ
2024/05/25 15:19
2 more properties

파이썬의 λ°μ½”λ ˆμ΄ν„°λž€?

μ–΄λ–€ ν•¨μˆ˜μ˜ λ‚΄λΆ€λ‘œμ§μ„ λ³€κ²½ν•˜μ§€ μ•Šκ³  ν•¨μˆ˜μ— κΈ°λŠ₯을 μΆ”κ°€ν•΄μ£ΌλŠ” 파이썬 ꡬ문이닀.
λ°μ½”λ ˆμ΄ν„°λŠ” κ·Έ 뜻 처럼 ν•¨μˆ˜λ₯Ό κΎΈλ©°μ£ΌλŠ” κΈ°λŠ₯이닀.

예제1

def decorator(fn): def wrapper(): print("deco start") fn() print("deco end") return wrapper def say(): print("say μ‹€ν–‰") say = decorator(say) say() >>> μ‹€ν–‰κ²°κ³Ό deco start say μ‹€ν–‰ deco end
Python
볡사
β€’
decorator(fn)
: fn ν•¨μˆ˜λ₯Ό λ§€κ°œλ³€μˆ˜λ‘œ λ°›μ•„μ„œ, wrapper ν•¨μˆ˜λ₯Ό λ¦¬ν„΄ν•œλ‹€.
β€’
say = decorator(say)
: say 객체λ₯Ό decorator() 의 λ§€κ°œλ³€μˆ˜λ‘œ 전달을 ν•΄μ„œ decorator() κ°€ λ¦¬ν„΄ν•˜λŠ” 값을 λ‹€μ‹œ say 객체에 ν• λ‹Ήν•œλ‹€.
파이썬의 ν•¨μˆ˜λŠ” 일급 객체이닀. λ”°λΌμ„œ, 1. ν•¨μˆ˜λ₯Ό λ³€μˆ˜μ— ν• λ‹Ήν•  수 μžˆλ‹€. 2. ν•¨μˆ˜λ₯Ό λ§€κ°œλ³€μˆ˜λ‘œ 전달가λŠ₯ν•˜λ‹€. 3. ν•¨μˆ˜λ₯Ό 리턴 κ°€λŠ₯ν•˜λ‹€.

예제2 - @ 심볼 μ‚¬μš© (예제1 κ°œμ„ )

def decorator(fn): def wrapper(): print("deco start") fn() print("deco end") return wrapper # say = decorator(say) 와 λ˜‘κ°™μ€ κΈ°λŠ₯ @decorator def say(): print("say μ‹€ν–‰") say() >>> μ‹€ν–‰κ²°κ³Ό deco start say μ‹€ν–‰ deco end
Python
볡사
예제1 κ³Ό μ‹€ν–‰κ²°κ³Όκ°€ λ˜‘κ°™λ‹€.
say ν•¨μˆ˜μ˜ μ„ μ–ΈλΆ€ μœ„μ— @decorator λ₯Ό λΆ™νžˆλ©΄, say = decorator(say) 와 λ˜‘κ°™μ€ κΈ°λŠ₯을 ν•˜κ²Œ λœλ‹€.

예제3 - μ›λž˜ ν•¨μˆ˜μ— μΈμžκ°€ μžˆμ„ λ•Œ

def decorator(fn): def wrapper(): print("deco start") fn() print("deco end") return wrapper @decorator def say(msg): print(msg) say("hi") >>> μ‹€ν–‰κ²°κ³Ό Traceback (most recent call last): File "/Users/shawn/dev/python/decorater.py", line 15, in <module> say("hi") TypeError: wrapper() takes 0 positional arguments but 1 was given
Python
볡사
TypeError: wrapper() takes 0 positional arguments but 1 was given
: say() ν•¨μˆ˜κ°€ 인자λ₯Ό λ°›κ²Œλ” μ„ μ–Έν•˜κ³ , say(”hi”) λ₯Ό μ‹€ν–‰ν•˜μ˜€λ”λ‹ˆ μ—λŸ¬κ°€ λ°œμƒν•œλ‹€.
decorator() 내뢀에 μžˆλŠ” wrapper() ν•¨μˆ˜κ°€ 인자λ₯Ό 받지 λͺ»ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.
wrapper() 와 fn() 이 인자λ₯Ό 받을 수 μžˆκ²Œλ” *args, **kwargs λ₯Ό 인자둜 λ„£μ–΄μ£Όλ©΄ μ •μƒμ μœΌλ‘œ μ‹€ν–‰λœλ‹€.
def decorator(fn): def wrapper(*args, **kwargs): print("deco start") fn(*args, **kwargs) print("deco end") return wrapper @decorator def say(msg): print(msg) say("hi") >>> μ‹€ν–‰κ²°κ³Ό deco start hi deco end
Python
볡사

예제4 - μ›λž˜ ν•¨μˆ˜μ— 리턴 값이 μžˆμ„ λ•Œ

def decorator(fn): def wrapper(*args, **kwargs): print("deco start") fn(*args, **kwargs) print("deco end") return wrapper @decorator def say(msg): print(msg) return "return say" result = say("hi") print(result) >>> μ‹€ν–‰κ²°κ³Ό deco start hi deco end None
Python
볡사
say() ν•¨μˆ˜μ—μ„œ λ¦¬ν„΄ν•œ 값이 좜λ ₯λ˜μ§€ μ•ŠλŠ”λ‹€.
wrapper ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ fn() 의 μ‹€ν–‰κ²°κ³Όλ₯Ό μ§€μ—­λ³€μˆ˜μ— ν• λ‹Ήν•˜κ³ , 리턴해주면 ν•΄κ²°λœλ‹€.
def decorator(fn): def wrapper(*args, **kwargs): print("deco start") value = fn(*args, **kwargs) print("deco end") return value return wrapper @decorator def say(msg): print(msg) return "return say" result = say("hi") print(result) >>> μ‹€ν–‰κ²°κ³Ό deco start hi deco end return say
Python
볡사
참고자료