• <i id='2o4BA'><tr id='2o4BA'><dt id='2o4BA'><q id='2o4BA'><span id='2o4BA'><b id='2o4BA'><form id='2o4BA'><ins id='2o4BA'></ins><ul id='2o4BA'></ul><sub id='2o4BA'></sub></form><legend id='2o4BA'></legend><bdo id='2o4BA'><pre id='2o4BA'><center id='2o4BA'></center></pre></bdo></b><th id='2o4BA'></th></span></q></dt></tr></i><div id='2o4BA'><tfoot id='2o4BA'></tfoot><dl id='2o4BA'><fieldset id='2o4BA'></fieldset></dl></div>
      • <bdo id='2o4BA'></bdo><ul id='2o4BA'></ul>

      <small id='2o4BA'></small><noframes id='2o4BA'>

        <tfoot id='2o4BA'></tfoot>

        <legend id='2o4BA'><style id='2o4BA'><dir id='2o4BA'><q id='2o4BA'></q></dir></style></legend>

        如何在python中做一个条件装饰器

        how to do a conditional decorator in python(如何在python中做一个条件装饰器)

        <legend id='FYIod'><style id='FYIod'><dir id='FYIod'><q id='FYIod'></q></dir></style></legend>

        <i id='FYIod'><tr id='FYIod'><dt id='FYIod'><q id='FYIod'><span id='FYIod'><b id='FYIod'><form id='FYIod'><ins id='FYIod'></ins><ul id='FYIod'></ul><sub id='FYIod'></sub></form><legend id='FYIod'></legend><bdo id='FYIod'><pre id='FYIod'><center id='FYIod'></center></pre></bdo></b><th id='FYIod'></th></span></q></dt></tr></i><div id='FYIod'><tfoot id='FYIod'></tfoot><dl id='FYIod'><fieldset id='FYIod'></fieldset></dl></div>
          <bdo id='FYIod'></bdo><ul id='FYIod'></ul>
          <tfoot id='FYIod'></tfoot>
              <tbody id='FYIod'></tbody>

              1. <small id='FYIod'></small><noframes id='FYIod'>

                  本文介绍了如何在python中做一个条件装饰器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以有条件地装饰函数.例如,我想用定时器函数(timeit)装饰函数foo()只有doing_performance_analysis是True(见伪代码下面).

                  Is it possible to decorator a function conditionally. For example, I want to decorate the function foo() with a timer function (timeit) only doing_performance_analysis is True (see the psuedo-code below).

                  if doing_performance_analysis:
                    @timeit
                    def foo():
                      """
                      do something, timeit function will return the time it takes
                      """
                      time.sleep(2)
                  else:
                    def foo():
                      time.sleep(2)  
                  

                  推荐答案

                  装饰器是简单的可调用函数,它返回一个替换,可选相同的函数、包装器或完全不同的东西.因此,您可以创建一个条件装饰器:

                  Decorators are simply callables that return a replacement, optionally the same function, a wrapper, or something completely different. As such, you could create a conditional decorator:

                  def conditional_decorator(dec, condition):
                      def decorator(func):
                          if not condition:
                              # Return the function unchanged, not decorated.
                              return func
                          return dec(func)
                      return decorator
                  

                  现在你可以像这样使用它了:

                  Now you can use it like this:

                  @conditional_decorator(timeit, doing_performance_analysis)
                  def foo():
                      time.sleep(2)  
                  

                  装饰器也可以是一个类:

                  The decorator could also be a class:

                  class conditional_decorator(object):
                      def __init__(self, dec, condition):
                          self.decorator = dec
                          self.condition = condition
                  
                      def __call__(self, func):
                          if not self.condition:
                              # Return the function unchanged, not decorated.
                              return func
                          return self.decorator(func)
                  

                  这里的__call__方法和第一个例子中返回的decorator()嵌套函数的作用一样,封闭的deccondition 参数在这里作为参数存储在实例上,直到应用装饰器.

                  Here the __call__ method plays the same role as the returned decorator() nested function in the first example, and the closed-over dec and condition parameters here are stored as arguments on the instance until the decorator is applied.

                  这篇关于如何在python中做一个条件装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  • <tfoot id='U37Rx'></tfoot>

                    1. <small id='U37Rx'></small><noframes id='U37Rx'>

                        <legend id='U37Rx'><style id='U37Rx'><dir id='U37Rx'><q id='U37Rx'></q></dir></style></legend>
                          <tbody id='U37Rx'></tbody>
                          <bdo id='U37Rx'></bdo><ul id='U37Rx'></ul>

                          • <i id='U37Rx'><tr id='U37Rx'><dt id='U37Rx'><q id='U37Rx'><span id='U37Rx'><b id='U37Rx'><form id='U37Rx'><ins id='U37Rx'></ins><ul id='U37Rx'></ul><sub id='U37Rx'></sub></form><legend id='U37Rx'></legend><bdo id='U37Rx'><pre id='U37Rx'><center id='U37Rx'></center></pre></bdo></b><th id='U37Rx'></th></span></q></dt></tr></i><div id='U37Rx'><tfoot id='U37Rx'></tfoot><dl id='U37Rx'><fieldset id='U37Rx'></fieldset></dl></div>