用于访问类变量的Python装饰器

时间:2018-10-31 17:16:00

标签: python

我有一个装饰器来控制时间限制,如果函数执行超出限制,则会引发错误。

def timeout(seconds=10):
    def decorator(func):
        # a timeout decorator
    return decorator

我想使用构造函数将时间限制传递给该类来构建一个类。

def myClass:
    def __init__(self,time_limit):
        self.time_limit = time_limit

    @timeout(self.time_limit)
    def do_something(self):
        #do something

但这不起作用。

File "XX.py", line YY, in myClass
    @timeout(self.tlimit)
NameError: name 'self' is not defined

实现此目的的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

self.time_limit仅在调用类实例中的方法时可用。

另一方面,装饰器语句(在方法之前)在分析类主体时运行。

但是,如果将修饰符的内部部分始终应用于方法,它将获得self作为其第一个参数-在那里您可以简单地使用任何实例属性:

def timeout(**decorator_parms):
    def decorator(func):
        def wrapper(self, *args, **kwargs):
             time_limit = self.time_limit
             now = time.time()
             result = func(self, *args, **kwargs)
             # code to check timeout
             ..
             return result
        return wrapper
    return decorator

如果预期装饰器可以使用除self.limit以外的其他时间限制,则可以始终传递字符串或其他常量对象,并使用简单的if语句在最里面的装饰器中检查它。如果超时是某个字符串或对象,请使用instance属性,否则请使用传入的值;

答案 1 :(得分:0)

您还可以在构造函数中修饰方法:

def myClass:
    def __init__(self,time_limit):
        self.do_something = timeout(time_limit)(self.do_something)
    
    def do_something(self):
        #do something
相关问题