본문 바로가기

Python/Python Basic

Decorator_03 (Decorator Chaining, Method decoration, Decorator with parameters)

Decorator Chaining

- 복수개의 decorator 적용 가능

- decorated 된 순서가 중요

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def star(fn):
    def wrapper(*args, **kwargs):
        print('function is decorated with **star**')
        return fn(*args, **kwargs)
    return wrapper
 
def at(fn):
    def wrapper(*args, **kwargs):
        print('function is decorated with @@at@@')
        return fn(*args, **kwargs)
    return wrapper
 
@at
@star
def print_hello():
    print('hello')
 
print_hello()
 
print('='*35)
 
@star
@at
def print_hello():
    print('hello')
    
print_hello()
 
==================<<실행결과>>==================
 
function is decorated with @@at@@
function is decorated with **star**
hello
===================================
function is decorated with **star**
function is decorated with @@at@@
hello
cs

 

 

Method decoration

- 객체의 method도 decorating 가능

- 객체의 경우 중첩 함수에서 최초의 파라미터를 self로 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def h1_tag(func):
    def func_wrapper(self, *args, **kwargs):
        return "<h1>{0}<h1>".format(func(self, *args, **kwargs))
    return func_wrapper
 
class Person(object):
    def __init__(self):
        self.first_name = 'Barbie'
        self.last_name = 'Oh'
        
    @h1_tag
    def get_name(self):
        return self.first_name + ' ' + self.last_name
    
    @h1_tag
    def get_x(self, x):
        return x * 2
    
# Class Person의 객체 생성
barbie = Person()
 
print(barbie.get_name())
print(barbie.get_x('HA'))
 
==================<<실행결과>>==================
 
<h1>Barbie Oh<h1>
<h1>HAHA<h1>
cs

 

 

Decorator with parameters

- decorator에 파라미터 추가 가능

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def h1_tag(func):
    def func_wrapper(self, *args, **kwargs):
        return '<h1>{0}<h1>'.format(func(self, *args, **kwargs))
    return func_wrapper
 
#print_hello = h1_tag(print_hello)
 
# 중첩 함수의 depth를 하나 더 두어서 생성
def star(star_num=20):
    def callable(fn):
        def wrapper(*args, **kwargs):
            print('function is decorated with {}'.format('*' * star_num))
            return fn(*args, **kwargs)
        return wrapper
    return callable
 
def print_hello():
    print('hello')
    
hello = star(5)(print_hello)
hello()
 
print('='*50)
 
hello2 = star()(print_hello)
hello2()
 
==================<<실행결과>>==================
 
function is decorated with *****
hello
==================================================
function is decorated with ********************
hello
cs

 

※ 위의 코드를 parameter가 있는 decorator화 할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def h1_tag(func):
    def func_wrapper(self, *args, **kwargs):
        return '<h1>{0}<h1>'.format(func(self, *args, **kwargs))
    return func_wrapper
#print_hello = h1_tag(print_hello)
# 중첩 함수의 depth를 하나 더 두어서 생성
def star(star_num=20):
    def callable(fn):
        def wrapper(*args, **kwargs):
            print('function is decorated with {}'.format('*' * star_num))
            return fn(*args, **kwargs)
        return wrapper
    return callable
def print_hello():
    print('hello')
    
@star(star_num=3)
def print_hello():
    print('hello')
    
print_hello()
 
==================<<실행결과>>==================
 
function is decorated with ***
hello
cs

 

반응형