Python 装饰器

1. 什么是装饰器?python

  顾名思义,装饰器就是在方法上方标一个带有@符号的方法名,以此来对被装饰的方法进行点缀改造。shell

  当你明白什么是装饰器以后,天然会以为这个名字取得恰如其分,但做为初学者来讲多少仍是会有些迷茫。下面用代码来讲明怎么理解装饰器。函数

#脚本1
def target():
    print(this is target)
    
def decorator(func):
    func()
    print(this is decorator)
    
decorator(target)
-------------------------------------------
    
运行结果为:
    
this is target
this is decorator

Python容许将方法看成参数传递,所以以上脚本就是将target方法做为参数传入decorator方法中,这其实也是装饰器的工做原理,以上代码等同于:学习

#脚本2
def decorator(func):
    func()
    print(this is decorator)
    
@decorator
def target():
    print(this is target)
    
target
-------------------------------------------
运行结果:
    
this is target
this is decorator

所以能够看出,所谓的装饰器就是利用了Python的方法可做参数传递的特性,将方法target做为参数传递到方法decorator中。this

@decorator
def target():
   ...对象

  这种在一个方法的上方加一个@符号的写法,就是表示位于下方的方法将被做为参数传递到位于@后面的decorator方法中。使用@符号只是让脚本1中的代码换了一个写法,更加好看,固然也会更加灵活与实用,后面会讲到这点。但它们的本质实际上是同样的,这也就是装饰器的工做原理。内存

  2. 装饰器的原理字符串

  若是你仔细看的话,会在脚本2中发现一个问题,那就是脚本2中最后一行的target只是一个方法名字,它不是正确的方法调用,正确写法应该加上左右括号的target(),以下:get

#脚本3
    
def decorator(func):
    func()
    print(this is decorator)
    
@decorator
def target():
    print(this is target)
    
target()
--------------------------------------------
运行结果:
    
this is target
this is decorator
Traceback (most recent call last):
  File "C:/Users/Me/Desktop/ff.py", line 34, in <module>
    target()
TypeError: NoneType object is not callable

正如你所看到的,若是按照正确的写法,运行结果你会看到应该出现的两行打印文字"this is target"和"this is decorator",还会出现错误提示,ff.py是我为写这篇心得临时编写的一个py脚本名字,提示说'NoneType'对象不可调用。这是怎么回事?好吧,我如今必须告诉你,其实脚本2和脚本3中并非一个使用装饰器的正确写法,不是使用错误,而是做为装饰器的decorator方法写的并不友好,是的,我不认为它是错误的写法,只是不友好。但只要你明白其中的道理,使用恰当的手段也是能够运行正常的,这就是为何脚本2看似写错了调用方法却得出了正确的结果。固然学习仍是得规规矩矩,后面我会具体说正确的装饰器怎么书写,在这里我先解释了一下脚本2和脚本3的运行原理,了解它们的运行原理和错误缘由,其实就是了解装饰器的原理。编译器

  脚本2和脚本3的区别在于target和target(),也就是说真正的差异在于()这个括号。当()被附加在方法或者类后面时,表示调用,或者称为运行及实例化,不管称呼怎样,本质意义没有不一样,都是调用给出的对象,当对象不具有调用性的时候,就会报错:'某个类型' object is not callable。当一个方法被调用后,即target(),是否能被再次执行,取决于它是否会return一个对象,而且该对象能够被调用。也许你会有点迷糊,对比一下代码会比较容易理解我想表达的意思:

>>>def returnme():
>>>    print(this is returnme)
     
>>>def target():
>>>    print(this is target)
      
>>>target
<function target at 0x00000000030A40D0>
      
>>>target()
target
<function returnme at 0x00000000030A4268>
     
>>>target()()
target
returnme
    
>>>returnme()()
returnme
Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    returnme()()
TypeError: NoneType object is not callable

如上所示,当直接在脚本中输入target,它只是告诉编译器(我想是编译器吧,由于我也不是很懂所谓编译器的部分),总之就是告诉那个不知道在哪一个角落控制着全部python代码运行的“大脑”,在

0x00000000030A40D0位置(这个位置应该是指内存位置)存有一个function(方法)叫target;在target后面加上(),表示调用该方法,即输入target(),“大脑”便按照target方法所写的代码逐条执行,因而打印出了target字符串,而且“大脑”明白在0x00000000030A4268位置有一个叫returnme的方法;由于target对象调用后是会返回一个returnme方法,而且方法是能够被调用的,所以你能够直接这样书写target()(),“大脑”会逐条执行target中的代码,而后return一个returnme,由于多加了一个(),表示要对返回的returnme进行调用,因而再次逐条执行returnme中的代码,最后便能看到1五、16的打印结果;而returnme方法是没有返回任何可调用的对象,所以当输入returnme()()时,“大脑”会报错。

  下面咱们能够来解释一下脚本2和脚本3的运行详情,以前说过,装饰器的工做原理就是脚本1代码所演示的那样。

@decorator
def target():
    ...
    
等同于
    
def decorator(target)():
    ...
    
注:python语法中以上写法是非法的,以上只是为了便于理解。

当你调用被装饰方法target时,其实首先被执行的是做为装饰器的decorator函数,而后“大脑”会把target方法做为参数传进去,因而:

#脚本2
def decorator(func):
    func()
    print(this is decorator)
      
@decorator
def target():
    print(this is target)
      
target
-------------------------------------------
    
实际运行状况:
首先调用decorator方法:decorator()
由于decorator方法含1个参数,所以将target传入:decorator(target)
运行代码“func()”,根据传入的参数,实际执行target(),结果打印出:this is target
运行代码"print(this is decorator)",结果打印出:this is decorator

对比脚本3的运行状况:

#脚本3
def decorator(func):
    func()
    print(this is decorator)
      
@decorator
def target():
    print(this is target)
      
target()
-------------------------------------------
    
实际运行状况:
首先调用decorator方法:decorator()
由于decorator方法含1个参数,所以将target传入:decorator(target)
运行代码“func()”,根据传入的参数,实际执行target(),结果打印出:this is target
运行代码"print(this is decorator)",结果打印出:this is decorator
    
以上与脚本2中运行状况彻底相同,接下来即是执行脚本2中target没有的(),也就是执行调用命令。
因为decorator(target)没有返回一个能够被调用的对象,所以“大脑”提示错误:NoneType object is not callable
相关文章
相关标签/搜索