python装饰器 语法糖

简介:

装饰器(Decorators)是 Python 的一个重要部分。简单地说:他们是修改其他函数的功能的函数。

比如说我们写flask,路由就是用装饰器定义的。如果写权限控制,那么权限控制一般也是由装饰器来实现的。日志记录,一般也可以通过装饰器来实现。

简单说,就是为了给某些函数增加一种或几种功能的做法。

下面举例实现。

一:基本函数

1.源码

from time import sleep


def watch_movie():
    print('看电影')
    sleep(3)
    print('The End')


if __name__ == '__main__':
    watch_movie()
View Code

2.执行结果

代码很简单,先打印看电影,间隔3秒,打印The End。

二:装饰器原理

1.目标:计算函数运行时间

2.源码

from time import sleep, time


def ceal_time():
    before = time()
    watch_movie()
    after = time()
    print('函数运行%s秒' % (after - before))


def watch_movie():
    print('看电影')
    sleep(3)
    print('The End')


if __name__ == '__main__':
    ceal_time()
View Code

3.执行结果

代码很简单,先打印看电影,间隔3秒,打印The End,然后打印函数运行计时。

4.分析

我们把一个函数放进另一个函数去运行,这就是装饰器的基本工作原理。

三:改造计时函数为通用函数

1.目标:把计算函数运行时间这个功能,适配给不同的函数。

2.源码

from time import sleep, time


def ceal_time(fun):
    before = time()
    fun()
    after = time()
    print('函数运行%s秒' % (after - before))


def watch_movie():
    print('看电影')
    sleep(3)
    print('The End')


def play_game():
    print('玩游戏')
    sleep(3)
    print('Game Over')


if __name__ == '__main__':
    ceal_time(watch_movie)
    ceal_time(play_game)
View Code

3.执行结果

看电影和玩游戏两个函数都执行了。

4.分析

我们可以把函数作为对象,传入另一个函数当中。

四:变为装饰器

1.目标:

我们改变了函数的调用方式,能不能不改变函数在调用位置的代码呢?

2.源码:

from time import sleep, time


def ceal_time(fun):
    def wrapper():
        before = time()
        fun()
        after = time()
        print('函数运行%s秒' % (after - before))

    return wrapper


@ceal_time
def watch_movie():
    print('看电影')
    sleep(3)
    print('The End')


# @ceal_time
def play_game():
    print('玩游戏')
    sleep(3)
    print('Game Over')


if __name__ == '__main__':
    watch_movie()
    play_game()
View Code

3.执行结果

看电影前面加了装饰器,实现了函数运行计时,玩游戏没有加装饰器,所以没有函数运行计时。

而且函数在main中的调用方式和没加装饰器是一样的。

五:函数有参数

1.目标:被装饰的函数,有参数的处理

2.源码:

from time import sleep, time


def ceal_time(fun):
    def wrapper(*args, **kwargs):  # 修改
        before = time()
        fun(*args, **kwargs)  # 修改
        after = time()
        print('函数运行%s秒' % (after - before))

    return wrapper


@ceal_time
def watch_movie(name, movie):
    print('%s在看%s电影' % (name, movie))
    sleep(3)
    print('The End')


# @ceal_time
def play_game(name, game):
    print('%s在玩%s游戏' % (name, game))
    sleep(3)
    print('Game Over')


if __name__ == '__main__':
    watch_movie(name='张三', movie='猫和老鼠')
    play_game(name='李四', game='魔兽争霸')
View Code

3.执行结果

4.

5.

6.

7.

8.

9.

 

六:

 

1.

2.

3.

4.

5.

6.

7.

8.

9.

 

七:

1.

2.

3.

4.

5.

6.

7.

8.

9.

 

八:

1.

2.

3.

4.

5.

6.

7.

8.

9.

 

九:

1.

2.

3.

4.

5.

6.

7.

8.

9.

 

猜你喜欢

转载自www.cnblogs.com/jackadam/p/11877034.html