๐ Function that remembers values in enclosing scope.
def print2(msg): def printer(): print(msg) return printer
add_number
ํจ์์ return ๊ฐ์ด ํจ์ adder
์ด๋ค.adder
์ ์ฌ์ฉ๋๋ num ๋ณ์๋ local variable์ด ์๋๋ฉฐ add_number
ํจ์์์ parameter๋ก ์ฌ์ฉ๋ ๊ฐ์ผ๋ก ์ฐ์ธ๋คdef add_number(num):
def adder(number):
print('adder is a closure')
return num + number
return adder
a_10 = add_number(10) #num = 10
a_20 = add_number(20) #num = 20
a1 = a_10(21) # prints 'adder is โฆ'
a2 = a_20(21) # prints 'adder is โฆ'
print(a1) #31
print(a2) #41
๐ Callable object that is used to modify a function or a class
def NewDecorator(func):
#@wraps(func) # ํจ์๋ช
๋ฐ ์ค๋ช
๋ฌธ ์ ์ง, ์์ผ๋ฉด(NewAdd)
def NewAdd(*args, **kwargs): #์ผ๋ฐํ๋ function arguments
print('Before call')
result = func(*args, **kwargs)
print('After call')
return result
return NewAdd
@NewDecorator #add ํจ์ callํ ๋ NewDecoratorํจ์๋ ํธ์ถ
def add(a, b):
print('Add')
return a + b
sum = add(1, 3) # โBefore callโ โAddโ โAfter callโ 4
#print(sum)
#print(add.__name__)
setter, getter, deleter