本帖最后由 jack_xy 于 2020-9-3 18:08 编辑
其实就是python程序解释程序的一个代码置换功能嘛,这篇文章有点绕。
(1) 定义某个类AClass() 或函数init_function()
class AClass(object):
def __init__(self,myfun):
self.f =myfun
def __call__(self):
....
self.f()
....
return xxxx
或者
def init_function(myfun):
def call():
.....
myfun()
.....
return xxxx
return call
(2)@某个类名AClass 或者 @某个函数名init_function
(3) 紧跟着定义(2)中类或者函数的参数,这个参数一般也是个函数指针myfun()
@AClass
def myfun():
....
.....
return aaaaa
或者
@init_function
def myfun():
....
.....
return aaaaa
(4)然后在程序代码段中调用这个参数函数myfun()时,编译程序实际上是调用(2)中这个类AClass(myfun).__call__()或者函数init_function(myfun) |