개발화이팅
데코레이터 사용1 본문
class Decorator1:
def __init__(self,f):
print("initializing Decorator1...")
self.func = f
#인스턴스 뒤에 ()붙여 호출하면 __call__메서드가 호출됨
def __call__(self):
print("Begin : {0}".format(self.func.__name__))
self.func()
print("End : {0}".format(self.func.__name__))
def print_hello():
print("Hello")
print_hello = Decorator1(print_hello)
print_hello()
decorator1의 인스턴스를 생성할 때 __init__() 메서드를 호출
func 데이터 속성이 print_hello를 받아둠
print_hello() 호출 => 인스턴스뒤에 () 붙여 호출 => __call__() 메서드 호출
class Decorator1:
def __init__(self,f):
print("initializing Decorator1...")
self.func = f
def __call__(self):
print("Begin : {0}".format(self.func.__name__))
self.func()
print("End : {0}".format(self.func.__name__))
@Decorator1
def print_hello():
print("Hello")
#print_hello = Decorator1(print_hello)
print_hello()
@Decorator1 붙여주게되면 #print_hello = Decorator1(print_hello) 역할을 하게된다
실행 결과
'python' 카테고리의 다른 글
모듈 (0) | 2019.08.22 |
---|---|
python_ 자료구조 (3) | 2019.08.20 |