Access self from decorator

前端 未结 1 1968
小鲜肉
小鲜肉 2020-11-29 01:48

In setUp() method of unittest I\'ve setup some self variables, which are later referenced in actual tests. I\'ve also created a decorator to do some logging. Is the

相关标签:
1条回答
  • 2020-11-29 02:10

    Since you're decorating a method, and self is a method argument, your decorator has access to self at runtime. Obviously not at parsetime, because there are no objects yet, just a class.

    So you change your decorator to:

    def decorator(func):
        def _decorator(self, *args, **kwargs):
            # access a from TestSample
            print 'self is %s' % self
            return func(self, *args, **kwargs)
        return _decorator
    
    0 讨论(0)
提交回复
热议问题