相关疑难解决方法(0)

Python functools.wraps等同于类

使用类定义装饰器时,如何自动转移__name__,__module____doc__?通常,我会使用functools的@wraps装饰器.这是我为一个课而做的(这不完全是我的代码):

class memoized:
    """Decorator that caches a function's return value each time it is called.
    If called later with the same arguments, the cached value is returned, and
    not re-evaluated.
    """
    def __init__(self, func):
        super().__init__()
        self.func = func
        self.cache = {}

    def __call__(self, *args):
        try:
            return self.cache[args]
        except KeyError:
            value = self.func(*args)
            self.cache[args] = value
            return value
        except TypeError:
            # uncacheable -- for instance, passing a list as an argument.
            # Better to not …
Run Code Online (Sandbox Code Playgroud)

python decorator

69
推荐指数
4
解决办法
2万
查看次数

装饰Python类方法 - 如何将实例传递给装饰器?

这是Python 2.5,它也是GAE,并不重要.

我有以下代码.我正在使用dec_check类作为装饰器在bar中装饰foo()方法.

class dec_check(object):

  def __init__(self, f):
    self.func = f

  def __call__(self):
    print 'In dec_check.__init__()'
    self.func()

class bar(object):

  @dec_check
  def foo(self):
    print 'In bar.foo()'

b = bar()
b.foo()
Run Code Online (Sandbox Code Playgroud)

执行此操作时,我希望看到:

In dec_check.__init__()
In bar.foo()
Run Code Online (Sandbox Code Playgroud)

但我得到" TypeError: foo() takes exactly 1 argument (0 given)"作为.foo()一种对象方法,以自我为参数.我猜测问题是bar当我执行装饰器代码时,实例并不存在.

那么如何将一个实例传递bar给装饰器类呢?

python python-decorators

59
推荐指数
3
解决办法
3万
查看次数

用decorator类装饰的方法没有冻结"self"参数

我有一个装饰器声明为一个类:

class predicated(object):
    def __init__(self, fn):
        self.fn = fn
        self.fpred = lambda *args, **kwargs: True

    def predicate(self, predicate):
        self.fpred = predicate
        return self

    def validate(self, *args, **kwargs):
        return self.fpred(*args, **kwargs)

    def __call__(self, *args, **kwargs):
        if not self.validate(*args, **kwargs):
            raise PredicateNotMatchedError("predicate was not matched")
        return self.fn(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

...当我用它来包装类中的方法时,调用该方法似乎并没有将对象的实例设置为第一个参数.虽然这种行为并非完全出乎意料,self但当方法成为实例方法时,我将如何进行冻结?


简化示例:

class test_decorator(object):
    def __init__(self, fn):
        self.fn = fn
    def __call__(self, *args, **kwargs):
        return self.fn(*args, **kwargs)

class Foo(object):
    @test_decorator
    def some_method(self):
        print(self)

Foo().some_method()
Run Code Online (Sandbox Code Playgroud)

foo的预期实例,而是得到一个错误,说0参数被传递.

python decorator

5
推荐指数
1
解决办法
537
查看次数

这个python装饰器是如何工作的?

编辑/澄清使我的问题特定于我的查询:*我可以看到如何调用装饰器静态日志函数,但我没有看到如何调用_以及它的结果如何是日志的结果.我想看看进入/进入的东西是如何工作的*

class logger:
    @staticmethod
    def log(func):
        def ___(*args, **kwargs):
            try:
                print "Entering: [%s] with parameters %s" % (func.__name__, args)
                try:
                    return func(*args, **kwargs)
                except Exception, e:
                    print 'Exception in %s : %s' % (func.__name__, e)
            finally:
                print "Exiting: [%s]" % func.__name__
        return ___


class x:
      @logger.log
      def first_x_method(self):
          print 'doing first_x_method stuff...'

x().first_x_method()
Run Code Online (Sandbox Code Playgroud)

给出这个输出:

Entering: [first_x_method] with parameters (<__main__.x instance at 0x0000000001F45648>,)
doing first_x_method stuff...
Exiting: [first_x_method]
Run Code Online (Sandbox Code Playgroud)

我可以看到logger是一个带有静态方法的类,用于装饰(@logger.log)first_x_method.

但是我不明白为什么___调用sub方法(并且它可以是任何名称).

python decorator

2
推荐指数
1
解决办法
642
查看次数

标签 统计

python ×4

decorator ×3

python-decorators ×1