TIL-077 | Python_ Closer & Decorator

Lee, ChankyuΒ·2022λ…„ 1μ›” 30일
0
post-thumbnail

🌈 Closer & Decorator

🧐 κ·Έλ™μ•ˆ ν”„λ‘œμ νŠΈλ₯Ό μ§„ν–‰ν•  λ•Œ μ—¬λŸ¬ κΈ°λŠ₯을 κ΅¬ν˜„ν•˜λ©° λ°μ½”λ ˆμ΄ν„°λ₯Ό ν™œμš©ν•˜κ³€ ν–ˆμ—ˆμ§€λ§Œ, ν΄λ‘œμ €λž€ 무엇인가? λ°μ½”λ ˆμ΄ν„°λž€ 무엇인가? λΌλŠ” μ§ˆλ¬Έμ„ μžμ‹ μ—κ²Œ 던져보면 만쑱슀러운 닡변이 νŠ€μ–΄ λ‚˜μ˜€μ§€ μ•ŠμŒμ„ μΈμ§€ν•˜μ˜€κΈ°μ— ν•œλ²ˆ 짚고 λ„˜μ–΄κ°€κ³ μž ν•œλ‹€.

Nested Function(μ€‘μ²©ν•¨μˆ˜)

  • ν΄λ‘œμ €μ™€ λ°μ½”λ ˆμ΄ν„°μ— λŒ€ν•΄ μ•Œμ•„λ³΄κΈ°μ „μ— ν•¨μˆ˜λ₯Ό μ€‘μ²©ν•΄μ„œ μ‚¬μš©ν•˜λŠ” μ€‘μ²©ν•¨μˆ˜μ™€ κ΄€λ ¨ν•΄μ„œ μ•„λž˜μ˜ μ˜ˆμ‹œλ₯Ό 보도둝 ν•˜μž.
def greeting():
    def hello():
        print("Hello!")
        
    hello()


>>> greeting()
Hello!
  • hello() ν•¨μˆ˜λŠ” λΆ€λͺ¨ν•¨μˆ˜ μ•ˆμ—μ„œλ§Œ μ‚¬μš©ν•  수 μžˆλ‹€.

  • μœ„μ™€ 같이 μ€‘μ²©ν•¨μˆ˜λ₯Ό μ“°λŠ” μ΄μœ μ—λŠ” 크게 가독성과 ν΄λ‘œμ €κ°€ μžˆλ‹€. λ°˜λ³΅λ˜λŠ” μ•‘μ…˜μ„ ν•¨μˆ˜ μ•ˆμ— μ •μ˜ ν•˜λ©΄ μ½”λ“œ 길이λ₯Ό 쀄일 수 μžˆλ‹€. ν΄λ‘œμ €μ— λŒ€ν•΄μ„œλŠ” μ•„λž˜μ—μ„œ μ„€λͺ…ν•˜λ„λ‘ ν•˜κ² λ‹€.


Closer (ν΄λ‘œμ €)

  • ν΄λ‘œμ €μ˜ μ‚¬μš©μ„ 톡해 λΆ€λͺ¨ν•¨μˆ˜μ˜ λ³€μˆ˜λ‚˜ 정보λ₯Ό κ°€λ‘”λ‹€.

  • μ–΄λ–€ 정보λ₯Ό 기반으둜 연산을 μ‹€ν–‰ν•˜κ³  μ‹ΆμœΌλ‚˜ κ·Έ μ •λ³΄μ˜ 접근을 μ œν•œν•˜μ—¬ λ…ΈμΆœμ΄λ‚˜ μˆ˜μ •μ„ λ§‰κ³ μž ν•  λ•Œ μ‚¬μš©ν•œλ‹€.

  • ν΄λ‘œμ €μ˜ μ„Έ κ°€μ§€ 쑰건
    1) Nested ꡬ쑰λ₯Ό κ°–μΆ°μ•Ό ν•œλ‹€.
    2) μ€‘μ²©ν•¨μˆ˜κ°€ λΆ€λͺ¨ν•¨μˆ˜μ˜ λ³€μˆ˜λ‚˜ 정보λ₯Ό μ€‘μ²©ν•¨μˆ˜ λ‚΄μ—μ„œ μ‚¬μš©ν•΄μ•Ό ν•œλ‹€.
    3) λΆ€λͺ¨ν•¨μˆ˜λŠ” return κ°’μœΌλ‘œ μ€‘μ²©ν•¨μˆ˜λ₯Ό return ν•΄μ•Ό ν•œλ‹€.

  • μ•„λž˜μ˜ μ˜ˆμ‹œλ₯Ό 보도둝 ν•˜μž.

def generate_power(base_number):    #line1
    def nth_power(power):           #line2
        return base_number ** power #line3
    return nth_power                #line4
    
>>> calculator = generate_power(2)
>>> calculator(5)
32
  • λ¨Όμ € generate_power(2)μ—μ„œ base_number '2'λ₯Ό 받은 μƒνƒœμ΄λ‹€.
  • calulator(5)λ₯Ό 톡해 powerλ₯Ό λ°›κ²Œλ˜κ³  2의 5승이 κ³„μ‚°λœλ‹€.

Decorator (λ°μ½”λ ˆμ΄ν„°)

  • Decoratorλž€, 'ν•¨μˆ˜λ₯Ό 인자둜 λ°›μ•„ μƒˆλ‘œμš΄ ν•¨μˆ˜λ₯Ό λ§Œλ“€μ–΄ λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜' 이닀.

  • μ’€ 더 μ‹€μ œ μ‚¬μš©κ³Ό μ—°κ΄€λ˜κ²Œ μ„€λͺ…ν•˜μžλ©΄, "ν•¨μˆ˜ μ‹€ν–‰ μ „ νŠΉμ •λ™μž‘μ„ ν•˜κ²Œ ν•˜λŠ” 것" 이라고 ν•  수 μžˆλ‹€.

import time

def perf_clock(func):
	def perf_clocked(*args):
		# μ‹œμž‘ μ‹œκ°„
		st = time.perf_counter()
		result = func(*args)
		# μ’…λ£Œ μ‹œκ°„
		et = time.perf_counter() - st
		# ν•¨μˆ˜λͺ…
		name = func.__name__
		# λ§€κ°œλ³€μˆ˜
		arg_str = ','.join(repr(arg) for arg in args)
		# 좜λ ₯ 
		print('Result : [%0.5fs] %s(%s) -> %r' %(et, name, arg_str,result))
		return result
	return perf_clocked
    
# @λ₯Ό μ΄μš©ν•΄μ„œ ν•¨μˆ˜λ₯Ό μž‘λ™ν•˜κΈ° 전에 λ°μ½”λ ˆμ΄ν„° ν•¨μˆ˜κ°€ μ•„λž˜ ν•¨μˆ˜λ₯Ό νŒŒλΌλ―Έν„°λ‘œ λ°›μ•„ μ‹€ν–‰ν•˜λŠ” ν˜•νƒœ

@perf_clock
def time_func(seconds):
    time.sleep(seconds)
@perf_clock
def sum_func(*numbers):
	return sum(numbers)
@perf_clock
def fact_func(n):
	return 1 if n < 2 else n * fact_func(n-1)
    
time_func(3) // # Result : [3.00306s] time_func(3) -> None
sum_func(1,2,3) // # Result : [0.00000s] sum_func(1,2,3) -> 6

λ°μ½”λ ˆμ΄ν„°μ˜ μž₯점

  • 쀑볡을 쀄이고 μ½”λ“œκ°€ κ°„κ²°ν•΄μ§„λ‹€
  • ν΄λ‘œμ €λ³΄λ‹€ 문법이 κ°„λ‹¨ν•˜λ‹€

λ°μ½”λ ˆμ΄ν„°μ˜ 단점

  • 디버깅이 νž˜λ“€λ‹€
  • μ—λŸ¬λ₯Ό μ°ΎκΈ° νž˜λ“€λ‹€.

πŸ“ Reference

  1. https://yulsay.tistory.com/128
  2. https://wikidocs.net/83687
  3. https://velog.io/@hyeseong-dev/Decorator-Closure
profile
Backend Developer - "Growth itself contains the germ of happiness"

0개의 λŒ“κΈ€