对Python装饰器decorator的理解

2017-05-11  本文已影响0人  乐行者123

前言

装饰器是个令人头疼的概念,下面我用最最简单的例子和最最详细的表达来解释一下这个概念,希望能对大家有所帮助。

装饰器

装饰器:用于在不改变原函数的情况下,给原函数扩张功能
解释:
我们先编写一个简单的打印字符串的函数,如下:

def print_ysf():
  print 'ysf'

调用函数print_ysf:

>>> print_ysf()
ysf

这个函数只有一个功能就是打印出ysf,但我现在要想在打印ysf之前先打印出hello,单不改变print_ysf函数,那怎么办呢,这个时候就要用到装饰器,
我先把具体用法写下来,再来解释.

def  decorator(func):
  def print_hello():
    print 'hello'
    return func()
  return print_hello
 
@decorator
def print_ysf():
  print 'ysf'

调用函数print_ysf

>>>print_ysf()
hello
ysf

神奇吧,这就是装饰器的功能,在没有改变print_ysf函数的情况下,增加了print_ysf函数的功能,下面我来解释一些其运行原理:
那我们现在看这个函数:

def  decorator(func):
  def print_hello():
    print 'hello'
    return func()
  return print_hello
 
@decorator
def print_ysf():
  print 'ysf'

首先记住一点:函数只有在调用时才被执行,因此乍一看改函数什么都没有被执行,但是因为有@decorator就不一样了

当python解释器扫到@decorator时会执执行如下步骤:

1.将@decorator下面的函数,即print_ysf函数作为decorator函数的参数,也就是decorator(print_ysf)

2.执行decorator(print_ysf)函数

3.现在执行decorator(print_ysf)函数,我们发现decorator(print_ysf)还有一个print_hello()函数,但因为没有被调用,因此不执行,
继续往下,那么其实就是执行了 return print_hello

4.将返回的print_hello函数赋给给@decorator下面的函数print_ysf,即print_ysf = print_hello

5.然后我们再命令行输入print_ysf(),其实相当于执行了print_hello(),那么就会按照print_hello函数的内部代码,
先print 'hello',然后return func(),而func就是decorato函数通过@decorator传进来的参数,即原本的print_ysf函数,于是就又执行了原本的
print_hello函数,因此会执行print ‘ysf’

6.所以最终输出为:

hello
ysf

后记

解释完毕,贫僧作为一个python初学者,已经尽力了,希望能对大家有所帮助吧。

上一篇下一篇

猜你喜欢

热点阅读