zoukankan      html  css  js  c++  java
  • python装饰器4【两个参数】【wraps】【不常规套路】

    1. 代码
    
    #  wraps的参数是原来的函数,修饰返回的函数
    
    def _implicit_lookup(self, subroutine, arg_name):  # feed 里面查找 animal
    
        replacer = ArgumentReplacer(subroutine, arg_name)
    
        @wraps(subroutine)  # 换汤不换药,不认识了subroutine
        def lookup_closure(*args, **kwargs):
            with replacer.replace_and_call(args, kwargs) as replaced:    # 【注意】不是*args, **kwargs
                if self._should_be_replaced(replaced.value):
                    self._lookup_in_store_and_replace_argument(replaced)
            return replaced.returned_value
        return lookup_closure
    
    1. 对比
    def a_new_decorator(a_func):
        
        @wraps(a_func)
        def wrapTheFunction(*args, **kwargs):
            print("I am doing some boring work before executing a_func()")
    
            a_func(*args, **kwargs)   # 【注意】不是 a_func(args, kwargs)
     
            print("I am doing some boring work after executing a_func()")
     
        return wrapTheFunction
    
    
    
  • 相关阅读:
    Asp.Net Core- 配置组件详解
    ASP.Net Core-依赖注入IoC
    ASP.Net Core-TagHelpers
    Selenium-等待
    Selenium-js
    Selenium-actions
    Selenium-基础操作
    Selenium-简介
    装饰者模式
    设计模式-策略者模式
  • 原文地址:https://www.cnblogs.com/amize/p/15035967.html
Copyright © 2011-2022 走看看