Python中装饰器的理解以及例题详解

一、什么是装饰器

网上有人是这么评价装饰器的,我觉得写的很有趣,比喻的很形象

  • 每个人都有的内裤主要是用来遮羞,但是到了冬天它没法为我们防风御寒,肿木办?
  • 我们想到的一个办法就是把内裤改造一下,让它变得更厚更长,这样一来,它不仅有遮羞功能,还能提供保暖,不过有个问题,这个内裤被我们改造成了长裤后,虽然还有遮羞功能,但本质上它不再是一条真正的内裤了。于是聪明的人们发明长裤
  • 在不影响内裤的前提下,直接把长裤套在了内裤外面,这样内裤还是内裤,有了长裤后再也不冷了

装饰器就像我们这里说的长裤,在不影响内裤作用的前提下,给我们的身子提供了保暖的功效。

二、装饰器的前传4步曲

为什么讲装饰器要放在入门篇的末尾讲呢,因为这货太难了,里面有许多前提要懂。Python的函数相信大家都懂了,但是大家知不知道函数也是一种对象,可以像参数一样传递,我们看下面的例子吧:

1)函数也是对象

def message(word='hello'):
    return word.upper()+'!'
print(message())
>>
HELLO!
my_message=message
print(my_message)
>>
<function message at 0x000000000288E198>

print(my_message())
>>
HELLO!

2)函数可以嵌套,定义在另外一个函数内部

def show():
    print 'Run in show()'
    def message(word='hello'):
       return word
    print(message())

show()
>>
Run in show()
hello

message可以嵌套在show函数里面,调用show的时候也会跑下message函数

一个函数还可以当做另外一个函数的返回值,不信看下面这个例子

def gteWordType(kind='lower'):
    def capitalize(word='hello'):
        return word.capitalize()+'!'
    def lower(word='hello'):
        return word.capitalize()+'...' 
    if kind == 'lower':
        return lower
    else:
        return capitalize

wordType = gteWordType()
print(wordType) 
print (wordType())

>>
<function gteWordType.<locals>.lower at 0x0000000001F24620>
Hello...

4)函数作为参数传入

我们先创建一个getName函数,然后把这个函数当做参数传递给foo函数

def getName(name='lq'):
    return name
def foo(func):
    print('i will call the getName function later')
    print(func())
foo(getName)
>>
i will call the getName function later
lq

三、装饰器的真面目

1).经过前面几步,大家应该理解了函数可以作为参数传入,也可以当参数返回,而且还可以嵌套装饰器其实就是对函数进行再次包装,它能够在不改变函数的前提下,增加函数的功能,可以在函数执行之前或者执行之后执行一段代码。

def my_new_decorator(a_function_to_decorate):
    def the_wrapper_around_the_original_function():
        print('Before the function runs')
        a_function_to_decorate()
        print('After the function runs')
def a_stand_alone_function():
    print('i am a a_stand_alone_function')
a_stand_alone_function()

a_stand_alone_function_decorated = my_new_decorator(a_stand_alone_function)
a_stand_alone_function_decorated()
>>
I am a stand alone function
Before the function runs
I am a stand alone function,don't you dare modify me
After the function runs

2).使用装饰器

@my_new_decorator
def another_stand_alone_function():
    print('leave me alone')

another_stand_alone_function()

装饰器有一个语法糖@,直接@my_new_decorator就把上面一坨代码轻松化解了,这就是Pythonic的代码,简洁高效.

其实相当于:

another_stand_alone_function=my_new_decorator(another_stand_alone_function)

使用装饰器的原因:

装饰器提供了一些和代码维护性和审美相关的优点。并且作为结构化工具,装饰器自然地促进了代码的封装,这减少了冗余性并使得未来维护和扩展变得更容易。

猜你喜欢

转载自blog.csdn.net/Lq_520/article/details/81668001
今日推荐