Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
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
Tags
more
Archives
Today
Total
관리 메뉴

개발화이팅

데코레이터 사용1 본문

python

데코레이터 사용1

dogfoot1 2019. 8. 21. 15:07

 

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