正在学习python3中, 对python的一些基础知识进行查漏补缺, 大神掠过
python的装饰器模式很简单, code如下
def test(fn):
def _(self, *args, **kwargs):
print("this is test")
fn(self, *args, **kwargs)
return _
class ClassB:
def __init__(self):
self.a = "Yan"
class ClassA(ClassB):
@test
def my_print(self, c: str):
print("hello word:{},{}".format(self.a, c))
if __name__ == '__main__':
a = ClassA()
a.my_print(c='New')
看下输出
this is test
hello word:Yan,New
意思很简单, 就是定义一个装饰器, test. test的参数是一个函数. 然后在执行这个函数之前, 先执行另外一个函数. 就这样.
看起来咋咋呼呼的, 就这.
这是一篇好文章, 记录下
https://zhuanlan.zhihu.com/p/93846887