python第2部分中的Decorator函数

2024-05-01 22:11:30 发布

您现在位置:Python中文网/ 问答频道 /正文

def request(method="GET", path="", data=None):
    def wrapper(func):
        func.routed = True
        func.method = method
        func.path = path
        func.data = data
        return func
    return wrapper

def response(fmt="%s", contentType="text/plain"):
    def wrapper(func):
        func.format = fmt
        func.contentType = contentType
        return func
    return wrapper


 @request("GET", "%(channel)d/value")
    @response("%d")
    def digitalRead(self, channel):
    self.checkDigitalChannel(channel)
    return self.__digitalRead__(channel)

从上次讨论开始,我们讨论了 @A @B类 定义函数:

会变成func=A()(B()func()),所以从上面看,@request和@response是包装器,那么新的digitalRead函数会是什么样子呢


Tags: pathselfdatagetreturnresponserequestdef