在调用方法之前检测方法是否已装饰

时间:2015-05-12 11:00:34

标签: python decorator python-decorators

我编写了一个与unittest.TestCase非常相似的Python流控制框架:用户创建一个派生自框架类的类,然后编写自定义task_*(self)方法。框架发现它们并运行它们:

###################
# FRAMEWORK LIBRARY
###################
import functools

class SkipTask(BaseException):
    pass

def skip_if(condition):
    def decorator(task):
        @functools.wraps(task)
        def wrapper(self, *args, **kargs):
            if condition(self):
                raise SkipTask()
            return task(self, *args, **kargs)
        return wrapper
    return decorator

class MyFramework(object):
    def run(self):
        print "Starting task"
        try:
            self.task()
        except SkipTask:
            print "Skipped task"
        except Exception:
            print "Failed task"
            raise
        else:
            print "Finished task"

#############
# USER SCRIPT
#############
class MyUserClass(MyFramework):
    skip_flag = True

    @skip_if(lambda self: self.skip_flag)
    def task(self):
        print "Doing something"

if __name__ == '__main__':
    MyUserClass().run()

输出:

Starting task
Skipped task

我想更改框架,以便在@skip_if的条件为True时,包装器不会打印"Starting task"。 我试过这个,但它不起作用:

def skip_if(condition):
    def decorator(task):
        print "decorating "  + str(task)
        task.__skip_condition = condition
        return task
    return decorator

class MyFramework(object):
    def run(self):
        try:
            if self.task.__skip_condition():
                print "Skipped task"
                return
        except AttributeError:
            print str(self.task) + " is not decorated"
            pass

        print "Starting task"
        try:
            self.task()
        except Exception as e:
            print "Failed task: " + str(e)
            raise
        else:
            print "Finished task"

输出:

decorating <function task at 0x194fcd70>
<bound method MyUserClass.task of <__main__.MyUserClass object at 0x195010d0>> is not decorated
Starting task
Doing something
Finished task

为什么不跳过任务?

1 个答案:

答案 0 :(得分:6)

您使用的是双下划线名称,其在run方法中已经过private name mangling

当单步执行调试器时,我得到:

AttributeError: "'function' object has no attribute '_MyFramework__skip_condition

此处不要使用双下划线名称;如果您将函数属性重命名为_skip_condition,则代码可以正常工作(只要您绑定条件函数或明确传入self):

def skip_if(condition):
    def decorator(task):
        print "decorating "  + str(task)
        task._skip_condition = condition
        return task
    return decorator

class MyFramework(object):
    def run(self):
        try:
            if self.task._skip_condition(self):
                print "Skipped task"
                return
        except AttributeError:
            print str(self.task) + " is not decorated"
            pass

        print "Starting task"
        try:
            self.task()
        except Exception as e:
            print "Failed task: " + str(e)
            raise
        else:
            print "Finished task"

通过这些更改,输出变为:

decorating <function task at 0x1071a1b90>
Skipped task