Decorator
def decorator(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
@decorator
def func():
pass
is roughly equivalent to
def func():
pass
func = decorator(func)
Log message before function
def log(message):
def log_wrapper(func):
def wrapper(*args, **kwargs):
print(message)
return func(*args, **kwargs)
return wrapper
return log_wrapper
@log('hello')
def world():
print('world')
is roughly equivalent to
def world():
print('world')
world = log('hello')(world)
world()
# hello
# world
Thread decorator
from threading import Thread
def thread(function):
def wrapper(*args, **kwargs):
return Thread(target=function, args=args, kwargs=kwargs)
return wrapper
@thread
def thread_function():
pass
thread_function().start()
with return value
from threading import Thread
from functools import partial
class ThreadFunction(Thread):
def __init__(self, target, args = (), kwargs = {}):
super().__init__()
self.target = partial(target, *args, **kwargs)
# function return value
self.value = None
def run(self):
self.value = self.target()
def thread(function):
def wrapper(*args, **kwargs):
return ThreadFunction(target=function, args=args, kwargs=kwargs)
return wrapper
@thread
def hello(name):
return 'hello ' + name
hello_thread = hello('world')
hello_thread.start()
hello_thread.join()
print(hello_thread.value)