元类编程
装饰器
任何时候你定义装饰器的时候,都应该使用 functools 库中的 @wraps 装饰器来注解底层包装函数.
因为一个普通装饰器作用在某个函数上时,这个函数的重要的元信息比如名字、文档字符串、注解和参数签名都会丢失。但是@wraps不会。
            
              
                import
              
               time
              
                from
              
               functools 
              
                import
              
               wraps
              
                def
              
              
                timethis
              
              
                (
              
              func
              
                )
              
              
                :
              
              
                '''
    Decorator that reports the execution time.
    '''
              
              
    @wraps
              
                (
              
              func
              
                )
              
              
                def
              
              
                wrapper
              
              
                (
              
              
                *
              
              args
              
                ,
              
              
                **
              
              kwargs
              
                )
              
              
                :
              
              
        start 
              
                =
              
               time
              
                .
              
              time
              
                (
              
              
                )
              
              
        result 
              
                =
              
               func
              
                (
              
              
                *
              
              args
              
                ,
              
              
                **
              
              kwargs
              
                )
              
              
        end 
              
                =
              
               time
              
                .
              
              time
              
                (
              
              
                )
              
              
                print
              
              
                (
              
              func
              
                .
              
              __name__
              
                ,
              
               end
              
                -
              
              start
              
                )
              
              
                return
              
               result
    
              
                return
              
               wrapper
            
          
        


 
					 
					