用于静态方法的Python写修饰符

用于静态方法的Python写修饰符,python,regex,caching,decorator,static-methods,Python,Regex,Caching,Decorator,Static Methods,我正在尝试创建一个函数,它将缓存它的一些属性,因为这些属性需要一些计算才能返回结果。因为这是相同的结果,所以我想以某种允许重用的方式缓存它 下面是一些代码,可以从这个角度进行分析: 这就是我建议的装饰师的样子: def cachedAttributes(func, **kwargs): func.__dict__.update(kwargs) return func 我试图装饰的类方法: class someclass: @staticmethod def me

我正在尝试创建一个函数,它将缓存它的一些属性,因为这些属性需要一些计算才能返回结果。因为这是相同的结果,所以我想以某种允许重用的方式缓存它

下面是一些代码,可以从这个角度进行分析:

这就是我建议的装饰师的样子:

def cachedAttributes(func, **kwargs):
    func.__dict__.update(kwargs)
    return func
我试图装饰的类方法:

class someclass:
    @staticmethod
    def method(*args):
        # compile regex operation
        # compile regex operation
        # compile regex operation

        # compute and return something
        ...
        pass
这就是我使用装饰器的方式

class someclass:
    @staticmethod
    @cachedAttributes(method, doctyperegex = re.compile(r'<!DOCTYPE.*?>\s*', re.S|re.U),
                  htmlregex = re.compile(r'<html.*?>\s*', re.S|re.U),
                  metaregex = re.compile(r'<meta(.*?)>', re.S|re.U))
    def method(*args):
        # compile regex operation
        # compile regex operation
        # compile regex operation

        # compute and return something
        ...
        pass

我知道这是因为在尝试装饰它之前没有定义该方法,但我如何才能做到这一点?有没有一个内置函数可以帮我做到这一点?

您需要遵循这个常见模式:您需要一个函数,它接受预期的参数并返回一个decorator函数

def cachedAttributes(**kwargs):
    def decorator(func):
        func.__dict__.update(kwargs)
        return func
    return decorator
然后:

class-someclass:
@静力学方法
@cachedAttributes(doctyperegex=re.compile(r'\s*',re.s|re.U),
htmlregex=re.compile(r'\s*',re.s|re.U),
metaregex=re.compile(r'',re.S | re.U))
def方法(*args):
#编译正则表达式操作
#编译正则表达式操作
#编译正则表达式操作
#计算并返回某物
...
通过

当Python应用decorator时,您的decorator将被传递
方法
函数。您不需要将方法作为参数提供给装饰器工厂:

@cachedAttributes(doctyperegex = re.compile(r'<!DOCTYPE.*?>\s*', re.S|re.U),
              htmlregex = re.compile(r'<html.*?>\s*', re.S|re.U),
              metaregex = re.compile(r'<meta(.*?)>', re.S|re.U))

因此Python调用了
cachedAttributes()
,它返回一个decorator。然后调用传递函数对象,返回值用作修饰函数的替换。这可能是原始函数对象,就像您在这里所做的那样。

谢谢,我知道嵌套函数的事情,但我不知道传递给它的参数是什么,所以我从没有包括它:“Decorator函数可以接受参数。如果提供了参数,则仅使用这些参数调用decorator函数,并且必须返回新的decorator函数;如前所述,此函数必须接受单个函数并返回一个函数。”
class someclass:
    @staticmethod
    @cachedAttributes(doctyperegex = re.compile(r'<!DOCTYPE.*?>\s*', re.S|re.U),
              htmlregex = re.compile(r'<html.*?>\s*', re.S|re.U),
              metaregex = re.compile(r'<meta(.*?)>', re.S|re.U))
    def method(*args):
        # compile regex operation
        # compile regex operation
        # compile regex operation

        # compute and return something
        ...
        pass
@cachedAttributes(doctyperegex = re.compile(r'<!DOCTYPE.*?>\s*', re.S|re.U),
              htmlregex = re.compile(r'<html.*?>\s*', re.S|re.U),
              metaregex = re.compile(r'<meta(.*?)>', re.S|re.U))
def cachedAttributes(**kwargs):
    def decorator(func):
        func.__dict__.update(kwargs)
        return func
    return decorator