Python修饰符
import timeclass Test:
def __init__(self):
self.name=''
self.functionDict = {}
def msg_register(self, msgType, isFriendChat=False, isGroupChat=False, isMpChat=False):
def _msg_register(fn):
self.functionDict = fn
return _msg_register
def do(self,msgType):
self.functionDict('abc',self.name)
a =Test()
a.name='111'
@a.msg_register('First')
def test(msg,tt):
print ('msg=',msg)
print (tt)
a.do('First')
这段代码里面修饰符部分怎么改成普通的函数形式? 将修饰符部分修改成普通函数形式的代码如下:
import time
class Test:
def __init__(self):
self.name=''
self.functionDict = {}
def msg_register(self, msgType, isFriendChat=False, isGroupChat=False, isMpChat=False):
def _msg_register(fn):
self.functionDict = fn
return _msg_register
def do(self,msgType):
self.functionDict('abc',self.name)
def test(msg,tt):
print('msg=',msg)
print(tt)
a = Test()
a.name='111'
a.msg_register('First')(test)
a.do('First')
在这个代码中,我们将修饰符@a.msg_register('First')替换成了a.msg_register('First')(test) ,以实现相同的功能。 isdkz 发表于 2023-9-20 12:04
将修饰符部分修改成普通函数形式的代码如下:
那a.msg_register('First')(test)中的('First')有什么用?
页:
[1]