Python 装饰器

发布于 2021-03-22 12:49:09 字数 8320 浏览 1165 评论 0

Python 函数是对象

To understand decorators, you must first understand that functions are objects in Python. This has important consequences. Let's see why with a simple example :

为了理解装饰器,首先需要明白函数在 Python 中也是对象,理解这一点很重要,下面用一个例子来说明:

def shout(word="yes"):
    return word.capitalize()+"!"

print shout()
# 输出 : 'Yes!'

# As an object, you can assign the function to a variable like any
# other object 
#函数作为对象,你可以象其他对象一样赋值给变量 
scream = shout

# Notice we don't use parentheses: we are not calling the function, we are
# putting the function "shout" into the variable "scream". 
# It means you can then call "shout" from "scream":
# 注意:这里我们没有使用圆括号去调用函数,仅仅是把函数"shout"赋值给变量"scream"
#也就是说你可以通过"scream"来调用"shout"
print scream()
# 输出 : 'Yes!'

# More than that, it means you can remove the old name 'shout', and
# the function will still be accessible from 'scream'
# 不仅如此,你还可以删除'shout',该函数仍然可以通过'scream'来访问。

del shout
try:
    print shout()
except NameError, e:
    print e
    #输出: "name 'shout' is not defined"

print scream()
# 输出: 'Yes!'

OK, keep that in mind, we are going back to it soon. Another interesting property of Python functions is they can be defined... inside another function!

好了,先记住上面规则,稍后再回到这里来,Python 另一个有意思的特点是函数可以定义在另一个函数里面。

def talk():

    # You can define a function on the fly in "talk" ...
    # 你可以直接在"talk"中定义函数
    def whisper(word="yes"):
        return word.lower()+"..."

    # ... and use it right away!
    # ... 立马就可以在这里使用 
    print whisper()

# You call "talk", that defines "whisper" EVERY TIME you call it, then
# "whisper" is called in "talk". 
# 每次调用"talk"时,"whipser"将被"talk"调用
talk()
# 输出: 
# "yes..."

# But "whisper" DOES NOT EXIST outside "talk":
# 注意:"whisper"在函数"talk"外面是不可见的 
try:
    print whisper()
except NameError, e:
    print e
    #输出 : "name 'whisper' is not defined"*

Functions references

OK, still here? Now the fun part, you've seen that functions are objects and therefore:

OK,你已经知道函数是对象了:

  • can be assigned to a variable;
  • 它可以赋值给变量
  • can be defined in another function.
  • 可以定义在另外的函数中

Well, that means that a function can return another function Have a look:

也就是意味着一个函数可以返回另一个函数,请看下面:

def getTalk(type="shout"):

    # We define functions on the fly
    # 直接在函数中定义函数
    def shout(word="yes"):
        return word.capitalize()+"!"

    def whisper(word="yes") :
        return word.lower()+"...";

    # Then we return one of them
    # 返回其中的一个函数
    if type == "shout":
        # We don't use "()", we are not calling the function,
        # 这里没有使用"()",我们不是调用这个函数,而是返回这个函数对象
        # we are returning the function object
        return shout  
    else:
        return whisper

# How do you use this strange beast?
# 怎么使用这个奇怪的东东呢?
# Get the function and assign it to a variable
# 获取函数并赋值给变量
talk = getTalk()      

# You can see that "talk" is here a function object:
  "talk"在这儿就是一个函数对象
print talk
#输出: <function shout at 0xb7ea817c>

# The object is the one returned by the function:
  这个对象就是由一个函数返回的 
print talk()
#输出 : Yes!

# And you can even use it directly if you feel wild:
你甚至能直接使用它
print getTalk("whisper")()
#输出 : yes...

But wait, there is more. If you can return a function, then you can pass one as a parameter:

等等,还有呢,如果能返回一个函数,那么这个函数还可以作为参数传递

def doSomethingBefore(func): 
    print "I do something before then I call the function you gave me"
    print func()

doSomethingBefore(scream)
#输出: 
#I do something before then I call the function you gave me
#Yes!

Well, you just have everything needed to understand decorators. You see, decorators are wrappers which means that they let you execute code before and after the function they decorate without the need to modify the function itself.

好啦,终于可以开始理解装饰器了,你瞧,装饰器就是对函数进行一层包裹,能在函数运行前或运行后执行额外的代码,而不需要修改函数本身。

Handcrafted decorators

纯手工装饰器

How you would do it manually:

你可以用手工方式如下实现:

A decorator is a function that expects ANOTHER function as parameter

装饰器是一个接受另一个函数作为参数的函数

def my_shiny_new_decorator(a_function_to_decorate):

    # Inside, the decorator defines a function on the fly: the wrapper.
    # This function is going to be wrapped around the original function
    # so it can execute code before and after it.
    # 
    # 装饰器内定义了一个函数,该函数包裹原始函数,可以在原始函数前后执行代码
    def the_wrapper_around_the_original_function():

        # Put here the code you want to be executed BEFORE the original 
        # function is called
        #在原始函数调用前执行代码
        print "Before the function runs"

        # Call the function here (using parentheses)
        #调用函数(注意这里带括号)
        a_function_to_decorate()

        # Put here the code you want to be executed AFTER the original 
        # function is called
        #调用完原始函数后执行代码
        print "After the function runs"

    # At this point, "a_function_to_decorate" HAS NEVER BEEN EXECUTED.
    # We return the wrapper function we have just created.
    # The wrapper contains the function and the code to execute before
    # and after. It's ready to use!
    # 在这个地方," a_function_to_decorate"函数从来没有被执行
    #而是返回这个刚刚创建的包裹函数,该函数包含了函数的代码及前后代码段,这样准备使用了
    return the_wrapper_around_the_original_function

# Now imagine you create a function you don't want to ever touch again.
# 现在假设你创建一个函数,而且又不想做任何修改了
def a_stand_alone_function():
    print "I am a stand alone function, don't you dare modify me"

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

# Well, you can decorate it to extend its behavior.
# Just pass it to the decorator, it will wrap it dynamically in 
# any code you want and return you a new function ready to be used:

#嗯,你可以装饰这个函数扩展其行为

a_stand_alone_function_decorated = my_shiny_new_decorator(a_stand_alone_function)
a_stand_alone_function_decorated()
#outputs:
#输出:
#Before the function runs
#I am a stand alone function, don't you dare modify me
#After the function runs

Now, you probably want that every time you call a_stand_alone_function, a_stand_alone_function_decorated is called instead. That's easy, just overwrite a_stand_alone_function with the function returned by my_shiny_new_decorator:

现在,你可能每次想每次调用a_stand_alone_function的时候,a_stand_alone_function_decorated就被调用,这很简单,只需重写a_stand_alone_function函数,通过my_shiny_new_decorator

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!

Decorators demystified

装饰器揭秘

The previous example, using the decorator syntax:
前面的例子,使用装饰器语法如下:

@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

Yes, that's all, it's that simple. @decorator is just a shortcut to:

没错,就这么简单,@装饰器 是下面代码的快捷方式:

another_stand_alone_function = my_shiny_new_decorator(another_stand_alone_function)

Decorators are just a pythonic variant of the decorator design pattern. There are several classic design patterns embedded in Python to ease development, like iterators.

装饰器是装饰器模式的一种pythonic方式,还有很多经典设计模式嵌在Python中简化开发,比如迭代器

Of course, you can cumulate decorators:

当然,你也可以累积装饰器

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()
#outputs: --ham--
sandwich = bread(ingredients(sandwich))
sandwich()
#outputs:
#</''''''\>
# #tomatoes#
# --ham--
# ~salad~
#<\______/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

JSmiles

生命进入颠沛而奔忙的本质状态,并将以不断告别和相遇的陈旧方式继续下去。

0 文章
0 评论
84961 人气
更多

推荐作者

醉城メ夜风

文章 0 评论 0

远昼

文章 0 评论 0

平生欢

文章 0 评论 0

微凉

文章 0 评论 0

Honwey

文章 0 评论 0

qq_ikhFfg

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文