Skip to content

Instantly share code, notes, and snippets.

@developer-sdk
Created September 7, 2018 00:29
Show Gist options
  • Select an option

  • Save developer-sdk/8d70b36e2b396da701d1420f34e929b1 to your computer and use it in GitHub Desktop.

Select an option

Save developer-sdk/8d70b36e2b396da701d1420f34e929b1 to your computer and use it in GitHub Desktop.
파이썬 데코레이터
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time, datetime
# 클래스 데코레이터
class ElapsedDecorator:
# 클래스 생성시 func에 입력
def __init__(self, f):
self.func = f
def __call__(self, *args, **kwargs):
print(datetime.datetime.now())
self.func(*args, **kwargs)
print(datetime.datetime.now())
# 함수 데코레이터
def start_end_decorator(f):
def dec_func(*args, **kwargs):
print(f.__name__ + "시작")
ret = f(args, kwargs)
print(f.__name__ + "종료")
return ret
return dec_func
@start_end_decorator
def print_hello1(*args, **kwargs):
print("hello1")
@ElapsedDecorator
@start_end_decorator
def print_hello2(*args, **kwargs):
print("hello2")
return "return hello2"
@ElapsedDecorator
def print_hello3(*args, **kwargs):
print("hello3")
print_hello1(1)
print("-------------------------------")
print(print_hello2(2))
print("-------------------------------")
print_hello3(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment