【问题标题】:Is @measured a standard decorator? What library is it in?@measured 是标准的装饰器吗?它在哪个图书馆?
【发布时间】:2010-09-27 19:29:50
【问题描述】:

this blog article 中,他们使用构造:

  @measured
  def some_func():
    #...
  # Presumably outputs something like "some_func() is finished in 121.333 s" somewhere

这个@measured 指令似乎不适用于原始python。这是什么?

更新:我从 Triptych 看到 @something 是有效的,但是我在哪里可以找到 @measured,它是在某个库中,还是这个博客的作者使用了他自己的私人代码库中的东西?

【问题讨论】:

    标签: python decorator


    【解决方案1】:

    是的,这是真的。它是一个函数装饰器。

    Python 中的函数装饰器是将函数作为单个参数并在其位置返回一个新函数的函数。

    @classmethod@staticmethod 是两个内置的函数装饰器。

    Read more »

    【讨论】:

      【解决方案2】:

      measured 是函数的名称,必须先定义该代码才能运行。

      一般来说,任何用作装饰器的函数都必须接受一个函数并返回一个函数。该函数将替换为将其传递给装饰器的结果 - 在本例中为 measure()。

      【讨论】:

      • 您的术语令人困惑。特别是生成器,在 Python 中有一个非常精确的定义。此外,虽然确实必须定义测量值,但这并不能真正解释它的作用。
      • 呸,是的。非常正确。它是一个装饰器,而不是一个生成器。错字已更正。我今天刚刚在大脑上安装了发电机。从给出的摘录中无法知道它的作用,所以我不确定是否可以解释它的作用。
      【解决方案3】:

      @measured 使用名为 measured 的函数或类来装饰 some_func() 函数。 @ 是装饰器语法,measured 是装饰器函数名。

      装饰器可能有点难以理解,但它们基本上用于将代码包装在函数周围,或者将代码注入其中。

      例如测量的函数(用作装饰器)可能是这样实现的......

      import time
      
      def measured(orig_function):
          # When you decorate a function, the decorator func is called
          # with the original function as the first argument.
          # You return a new, modified function. This returned function
          # is what the to-be-decorated function becomes.
      
          print "INFO: This from the decorator function"
          print "INFO: I am about to decorate %s" % (orig_function)
      
          # This is what some_func will become:
          def newfunc(*args, **kwargs):
              print "INFO: This is the decorated function being called"
      
              start = time.time()
      
              # Execute the old function, passing arguments
              orig_func_return = orig_function(*args, **kwargs)
              end = time.time()
      
              print "Function took %s seconds to execute" % (end - start)
              return orig_func_return # return the output of the original function
      
          # Return the modified function, which..
          return newfunc
      
      @measured
      def some_func(arg1):
          print "This is my original function! Argument was %s" % arg1
      
      # We call the now decorated function..
      some_func(123)
      
      #.. and we should get (minus the INFO messages):
      This is my original function! Argument was 123
      # Function took 7.86781311035e-06 to execute
      

      装饰器语法只是执行以下操作的一种更短更简洁的方式:

      def some_func():
          print "This is my original function!"
      
      some_func = measured(some_func)
      

      Python 中包含一些装饰器,例如 staticmethod - 但 measured 不是其中之一:

      >>> type(measured)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      NameError: name 'measured' is not defined
      

      检查项目import 语句以查看函数或类的来源。如果它使用from blah import *,您需要检查所有这些文件(这就是不鼓励使用import * 的原因),或者您可以执行grep -R def measured * 之类的操作

      【讨论】:

      • 我认为测量不是标准功能,但感谢您为我写了一个!
      • 我注意到示例代码中没有提到函数参数,这是否意味着它们被 Python 自动“正确”处理了?
      • 我一直在寻找描述符的一个很好的解释,这个为我做了。谢谢
      • unwind:好点,我忘了争论。修复了将所有参数传递给“包装”函数的代码
      猜你喜欢
      • 2016-03-30
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多