python 把类当作 装饰器

# 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----")

 

 


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

posted @ 2018-08-09 15:28  红尘陌上,独自行走  阅读(154)  评论(0编辑  收藏  举报