python 把类当作 装饰器

时间:2023-03-08 21:42:01

# class Test(object):
  # def __call__(self):
    # print('-----test----')

# t= Test()
# t() 调用主要有个__call__方法

# __new__ __init__ __del__ __str__ __slots__ __call__

class Test(object):
  def __init__(self, func):
    print("---初始化---")
    print("func name is %s"%func.__name__)
    self.__func = func
  def __call__(self):
    print("--z装饰器中的功能---")
    self.__func()

def __news__(self):
  print('---------')

@Test #相当于创建了一个对象
def test():
  print("----test----")

python 把类当作 装饰器

test()
# ---初始化---
# func name is test
# --z装饰器中的功能---
# ----test----