美文网首页ITS·黑客
Python学习笔记十一-装饰器2

Python学习笔记十一-装饰器2

作者: 6156fc232124 | 来源:发表于2017-04-22 21:34 被阅读19次

为了进一步明确装饰器的用法,结合我找的笔记,记录一个手工装饰器的例子,通过这个例子很容易理解装饰器的原理,并运用它。

# 装饰器是一个函数,而其参数为另外一个函数

def my_shiny_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"

#在此刻,"a_function_to_decrorate"还没有被执行,我们返回了创建的封装函数

#封装器包含了函数以及其前后执行的代码,其已经准备完毕

return the_wrapper_around_the_original_function

# 现在想象下,你创建了一个你永远也不远再次接触的函数

def a_stand_alone_function() :

print "I am a stand alone function, don't you dare modify me"

a_stand_alone_function()

#输出: I am a stand alone function, don't you dare modify me

# 好了,你可以封装它实现行为的扩展。可以简单的把它丢给装饰器

# 装饰器将动态地把它和你要的代码封装起来,并且返回一个新的可用的函数。

a_stand_alone_function_decorated = my_shiny_new_decorator(a_stand_alone_function)

a_stand_alone_function_decorated()

#输出 :

#Before the function runs

#I am a stand alone function, don't you dare modify me

#After the function runs

现在你也许要求当每次调用a_stand_alone_function时,实际调用却是a_stand_alone_function_decorated。实现也很简单,可以用my_shiny_new_decorator来给a_stand_alone_function重新赋值。

a_stand_alone_function = my_shiny_new_decorator(a_stand_alone_function)

a_stand_alone_function()

#输出 :

#Before the function runs

#I am a stand alone function, don't you dare modify me

#After the function runs

# And guess what, that's EXACTLY what decorators do !

这个时候,你是不是明白装饰器的用法了,但是!!!!如果你以为这样就结束了,那么可能你需要检讨一下自己是否学会装饰器,必要时候可以发个红包反省一下。

为什么呢?因为这里还没有用装饰器的语法@呀!!!!

那把例子改成装饰器的语法,是什么样子呢?

@my_shiny_new_decorator

def another_stand_alone_function() :

print "Leave me alone"

another_stand_alone_function()

#输出 :

#Before the function runs

#Leave me alone

#After the function runs

如果上面的搞懂了,那就一定明白装饰器了,那机智的我们再来升级一下!!!来一个“复合函数”试一下!!!

def bread(func) :

def wrapper() :

print ""

func()

print "<\______/>"

return wrapper

def ingredients(func) :

def wrapper() :

print "#tomatoes#"

func()

print "~salad~"

return wrapper

def sandwich(food="--ham--") :

print food

sandwich()

#输出 : --ham--

sandwich = bread(ingredients(sandwich))

sandwich()

#outputs :

#

# #tomatoes#

# --ham--

# ~salad~

#<\______/>

使用python装饰器语法:

@bread

@ingredients

def sandwich(food="--ham--") :

print food

sandwich()

#输出 :

#

# #tomatoes#

# --ham--

# ~salad~

#<\______/>

一定要注意@的前后顺序,其实就是“符合函数”谁在括号内谁在括号外。

前面只要掌握了,装饰器就没什么问题,后面的装饰器分类和应用只要看廖雪峰老师的教程就可以理解了,这里就不再赘述了。大家看了都懂了的,并且我学习起来没问题就不赘述了吧哈哈哈哈哈哈。

相关文章

  • Python学习笔记十一-装饰器2

    为了进一步明确装饰器的用法,结合我找的笔记,记录一个手工装饰器的例子,通过这个例子很容易理解装饰器的原理,并运用它...

  • python学习笔记十一(装饰器)

    装饰器 装饰器(语法糖例子) 对封装单元(如函数),不改变单元本身实现,通过装饰器改变行为其代码复用

  • 大师兄的Python学习笔记(十四): 迭代器、生成器和协程

    大师兄的Python学习笔记(十三): 理解装饰器大师兄的Python学习笔记(十五): Socket编程 一、关...

  • Python ☞ day 5

    Python学习笔记之 装饰器& 偏函数 & 异常处理 & 断言 & 文件读写 &编码与解码 装饰器 概念:是一个...

  • Python—闭包与装饰器

    将之前学习Python的笔记整理记录下来。 闭包 装饰器

  • 装饰器模式

    介绍 在python装饰器学习 这篇文章中,介绍了python 中的装饰器,python内置了对装饰器的支持。面向...

  • python中的装饰器

    python装饰器详解 Python装饰器学习(九步入门) 装饰器(decorator) 就是一个包装机(wrap...

  • Python装饰器

    Python装饰器 一、函数装饰器 1.无参装饰器 示例:日志记录装饰器 2.带参装饰器 示例: 二、类装饰器 示例:

  • TypeScript: 类的装饰器(三)

    带参数的类的装饰器 学习 python 的同学应该知道,python 中也有装饰器,而且 python 中的众多框...

  • python学习笔记----装饰器

    装饰器 实质: 是一个函数参数:是你要装饰的函数名(并非函数调用)返回:是装饰完的函数名(也非函数调用)作用:为已...

网友评论

    本文标题:Python学习笔记十一-装饰器2

    本文链接:https://www.haomeiwen.com/subject/eefezttx.html