类当做装饰器

在python中,只要某个对象重写了__call__()方法,那么这个对象就是callable的

 1 class Test(object):
 2     def __init__(self,func):
 3         print("初始化")
 4         print("func name is %s"%func.__name__)
 5         self.__func = func
 6     def __call__(self):
 7         print("装饰器中的功能")
 8         self.__func()
 9 @Test        
10 def test():
11     print("...test...")
12 
13 test()

猜你喜欢

转载自www.cnblogs.com/daxinzhe/p/10908342.html