我一直在尝试创建一个可以在python中与函数和方法一起使用的装饰器.它本身就不那么难,但是当创建一个带参数的装饰器时,它似乎就是这样.
class methods(object):
def __init__(self, *_methods):
self.methods = _methods
def __call__(self, func):
def inner(request, *args, **kwargs):
print request
return func(request, *args, **kwargs)
return inner
def __get__(self, obj, type=None):
if obj is None:
return self
new_func = self.func.__get__(obj, type)
return self.__class__(new_func)
Run Code Online (Sandbox Code Playgroud)
上面的代码正确地包装了函数/方法,但是在方法的情况下,request参数是它正在操作的实例,而不是第一个非自身参数.
有没有办法判断装饰器是否应用于函数而不是方法,并相应地处理?
我正在使用python-telegram-botpython3.x 库用 python 编写一个 Telegram 机器人,它是一个仅供私人使用的机器人(我和一些亲戚),所以我想阻止其他用户使用它。我的想法是创建一个授权用户 ID 列表,并且机器人不得回复从不在列表中的用户收到的消息。我怎样才能做到这一点?
编辑:我对 python 和python-telegram-bot. 如果可能的话,我希望有一个代码片段作为示例 =)。
arguments ×1
decorator ×1
function ×1
methods ×1
python ×1
python-3.x ×1
restriction ×1
telegram ×1