tuoheng_algN/test/语法/回调.py

40 lines
973 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class TestClass(object):
def __init__(self):
self.fun = 0
def decorator(self, fun):
#通过fun变量实现对传入函数的回调
self.fun = fun
#仅注册回调函数,将原函数返回,不执行原函数工鞥呢,不改变原函数功能
return fun
def decorator1(self, type):
def wrapper(func):
if type == 'A':
self.fun_A = func
if type == 'B':
self.fun_B = func
return func
return wrapper
test = TestClass()
#将decorator_test函数传入test的decorator函数并执行decorator函数
@test.decorator
def decorator_test():
print('this is decorator_test')
@test.decorator1('A')
def decorator_test():
print('this is decorator_test A')
@test.decorator1('B')
def decorator_test():
print('this is decorator_test B')
#通过test的fun变量回调decorator_test函数
test.fun();
test.fun_A ();
test.fun_B ();