这样用装饰器,为什么不行?

开发 前端
最近几周,陆续收到几位读者关于装饰器使用的提问:想要自定义一个Python装饰器,问我这样写装饰器行不行?如果不行,那又是为什么?今天统一回复。

最近几周,陆续收到几位读者关于装饰器使用的提问,今天统一回复。

[[391225]]

1. 问题

大概问题是这样,想要自定义一个Python装饰器,问我这样写装饰器行不行?如果不行,那又是为什么?

  1. import datetime 
  2. import time 
  3.  
  4. def print_time(g): 
  5.     def f(): 
  6.         print('开始执行时间') 
  7.         print(datetime.datetime.today()) 
  8.          
  9.         g() 
  10.          
  11.         print('结束时间') 
  12.         print(datetime.datetime.today()) 
  13.     f() 

下面使用 print_time装饰函数 foo:

  1. @print_time 
  2. def foo(): 
  3.     time.sleep(2) 
  4.     print('hello world') 

当调用 foo函数时,抛出如下异常:

  1. foo() 
  2.  
  3. --------------------------------------------------------------------------- 
  4. TypeError                                 Traceback (most recent call last) 
  5. <ipython-input-27-c19b6d9633cf> in <module> 
  6. ----> 1 foo() 
  7.  
  8. TypeError: 'NoneType' object is not callable 

所以,按照如上定义 print_time装饰器,肯定是不行的。

2. 为什么不行

要想明白为啥不行,首先要知道装饰器这个语法的本质。其实很简单,@print_time装饰foo函数等于:

  1. foo = print_time(foo) 

就是这一行代码,再也没有其他。

因为上面的 print_time 无返回值,所以赋值给 foo 函数后,foo 函数变为 None,所以当调用 foo() 时抛出 'NoneType' object is not callable

这也就不足为奇了。

3. 应该怎么写

print_time 需要返回一个函数,这样赋值给 foo函数后,正确写法如下所示:

  1. import datetime 
  2. import time 
  3.  
  4. def print_time(g): 
  5.     def f(): 
  6.         print('开始执行时间') 
  7.         print(datetime.datetime.today()) 
  8.          
  9.         g() 
  10.          
  11.         print('结束时间') 
  12.         print(datetime.datetime.today()) 
  13.     return f 

装饰 foo:

  1. @print_time 
  2. def foo(): 
  3.     time.sleep(2) 
  4.     print('hello world') 

调用 foo ,运行结果如下:

  1. foo() 
  2.  
  3. 开始执行时间 
  4. 2021-04-02 22:32:49.114124 
  5. hello world 
  6. 结束时间 
  7. 2021-04-02 22:32:51.119506 

一切正常

4. 装饰器好处

上面自定义print_time装饰器,除了能装饰foo函数外,还能装饰任意其他函数和类内方法。

装饰任意一个函数 foo2:

  1. @print_time 
  2. def foo2(): 
  3.   print('this is foo2') 

装饰类内方法 foo3,需要稍微修改原来的print_time:

  1. def print_time(g): 
  2.     def f(*args, **kargs): 
  3.         print('开始执行时间') 
  4.         print(datetime.datetime.today()) 
  5.      
  6.         g(*args, **kargs) 
  7.      
  8.         print('结束时间') 
  9.         print(datetime.datetime.today()) 
  10.     return f 

为类MyClass中foo3方法增加print_time装饰:

  1. class MyClass(object): 
  2.     @print_time 
  3.     def foo3(self): 
  4.         print('this is a method of class') 

执行结果如下:

  1. MyClass().foo3() 
  2.  
  3. 开始执行时间 
  4. 2021-04-02 23:16:32.094025 
  5. this is a method of class 
  6. 结束时间 
  7. 2021-04-02 23:16:32.094078 

以上就是装饰器的通俗解释,平时可以多用用,让我们的代码更加精炼、可读。

 

责任编辑:赵宁宁 来源: Python与算法社区
相关推荐

2017-07-07 17:01:32

装饰器代码Python

2023-06-26 07:31:29

中文编程编码

2013-01-22 09:35:27

Hadoop存储

2021-02-14 13:38:17

Python开发函数

2021-07-21 09:35:36

switchbreakJava

2021-05-08 08:55:54

CPUIBMIntel

2015-08-06 10:19:19

编程脑子

2011-07-21 11:11:10

Scrum

2010-04-06 12:59:18

MVC

2019-08-15 16:48:30

2021-01-20 12:43:07

编程语言Java

2021-01-20 12:44:22

JAVA编程语言软件

2015-09-07 09:53:02

Objective-CRuntime

2022-09-24 09:52:42

TopicQueuekafka

2023-02-07 07:47:52

Python装饰器函数

2010-02-01 17:50:32

Python装饰器

2017-06-19 13:10:59

大数据大数据平台架构

2021-02-01 15:51:45

数据可视化图表项目

2012-04-11 09:19:08

Haskell编程

2017-06-20 09:54:18

大数据架构数据分析
点赞
收藏

51CTO技术栈公众号