如何创建一个装饰器 class,它不接受任何参数并且可以被另一个 class 导入以进行 API 身份验证
How to create a decorator class that does not take any argument and can be imported by another class for API authentication
我的用户案例是我想创建一个我可以在整个应用程序中使用的装饰器 Util,我希望它像一个模块一样,它不应该有任何参数。写函数的例子很多,但是class.
的例子很少
'''
从烧瓶导入 make_response,jsonify
导入函数工具
class IsAdmin(object):
def __init__(self, arg):
self._arg = arg #--> self._args will hold the method
functools.update_wrapper(self, arg)
def __call__(self, *param_arg): #param_arg will hold the method params(2,2)
(status,msg) = util.isOwner()
if status == True and msg == 'is Owner':
#If there are decorator arguments, __call__() is only called onceas part of the decoration process. You can only give it a single argument,which is the function objectIf there are no decorator arguments, the functionto be decorated is passed to the constructor.>
# if len(param_arg) == 1:
# def wrapper(*main_args): #--> here we are passing the values that we pass to the function
# retval = param_arg[0](self, main_args[0], main_args[1])
# return retval ** self._arg[0]
# return wrapper
# else:
# expo = 2
retval = self._arg(self, *param_arg)
return retval
else:
return "you are not the admin"
'''
我的用户案例是我想创建一个我可以在整个应用程序中使用的装饰器 Util,我希望它像一个模块一样,它不应该有任何参数。写函数的例子很多,但是class.
的例子很少''' 从烧瓶导入 make_response,jsonify 导入函数工具
class IsAdmin(object):
def __init__(self, arg):
self._arg = arg #--> self._args will hold the method
functools.update_wrapper(self, arg)
def __call__(self, *param_arg): #param_arg will hold the method params(2,2)
(status,msg) = util.isOwner()
if status == True and msg == 'is Owner':
#If there are decorator arguments, __call__() is only called onceas part of the decoration process. You can only give it a single argument,which is the function objectIf there are no decorator arguments, the functionto be decorated is passed to the constructor.>
# if len(param_arg) == 1:
# def wrapper(*main_args): #--> here we are passing the values that we pass to the function
# retval = param_arg[0](self, main_args[0], main_args[1])
# return retval ** self._arg[0]
# return wrapper
# else:
# expo = 2
retval = self._arg(self, *param_arg)
return retval
else:
return "you are not the admin"
'''