Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这是Python中某种类型的装饰器吗?_Python_Decorator - Fatal编程技术网

这是Python中某种类型的装饰器吗?

这是Python中某种类型的装饰器吗?,python,decorator,Python,Decorator,我一直在阅读一些代码,在下面遇到了这个问题 我不明白@SdServer.appId(APP_ID)是否是装饰程序。它的@from是decorator,但是类方法appId看起来不像我习惯的decorator语法。我不明白这是怎么回事 在SdApp类中查找appID的最后包含的print语句返回以下结果: SdApp class instance ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__

我一直在阅读一些代码,在下面遇到了这个问题

我不明白@SdServer.appId(APP_ID)是否是装饰程序。它的@from是decorator,但是类方法appId看起来不像我习惯的decorator语法。我不明白这是怎么回事

在SdApp类中查找appID的最后包含的print语句返回以下结果:

SdApp class instance ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'request'] 

SdApp instance request ['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__func__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] 

method list ['request']
代码
classmethod本身不是装饰器,而是它的返回值。在您的示例中,
@SdServer.appId(APP_ID)
将调用classmethod并将结果用作装饰器。按照您的示例,这将是
\u handler
函数,它似乎将修饰类注册到
SdServer
类中。这个返回的decorator包含
cls
appId
变量上的闭包,因此实现有些复杂

是的,它是一个decorator@dangee1705,是否只是我没有正确调用该类以查看使用装饰器的结果?它在您传递的类中注入属性APP_ID_HANDLERS[appId]=f我相信我不是100%确定,由于我对decorators和class的使用不是很有经验,请看一下
SdServer.APP\u ID\u HANDLERS
class属性。例如,将
print(SdServer.APP\u ID\u HANDLERS)
放在定义
classsdapp
的部分之前和之后。
APP_ID = 'oA'

class SdServer(object):
    APP_ID_HANDLERS = {}

    def __init__(self, originator):
        self.originator = originator

    @classmethod
    def appId(cls, appId):
        def _handler(f):
            cls.APP_ID_HANDLERS[appId] = f
            return f

        return _handler


@SdServer.appId(APP_ID)
class SdApp(object):
    @classmethod
    def request(cls, originator, body=None):
        try:
            print(cls)
        except OException as e:
            log.error('Cannot process request: %s', e)

# me trying to figure out what it is doing below

first = SdApp()

print('SdApp class instance', dir(first), '\n')
print('SdApp instance request', dir(first.request), '\n')

method_list = [func for func in dir(SdApp) if callable(getattr(SdApp, func)) and not func.startswith("__")]

print('method list', method_list)