Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!

Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount

装饰器


```markdown 装饰器允许您对可调用对象进行简单修改,比如函数方法或类。在本教程中我们将处理函数。语法

@decorator
def functions(arg):
    return "value"

等同于:

def function(arg):
    return "value"
function = decorator(function) # 这会将函数传递给装饰器,并将其重新赋值给函数

正如您所见,装饰器只是另一个函数,它接受一个函数并返回一个函数。例如,您可以这样做:

def repeater(old_function):
    def new_function(*args, **kwds): # 参见 learnpython.org/en/Multiple%20Function%20Arguments 了解 *args 和 **kwds 的功能
        old_function(*args, **kwds) # 我们运行旧函数
        old_function(*args, **kwds) # 我们重复运行两次
    return new_function # 必须返回 new_function,或者不会重新赋值

这将使函数重复运行两次。

>>> @repeater
def multiply(num1, num2):
    print(num1 * num2)

>>> multiply(2, 3)
6
6

您还可以更改输出

def double_out(old_function):
    def new_function(*args, **kwds):
        return 2 * old_function(*args, **kwds) # 修改返回值
    return new_function

更改输入

def double_Ii(old_function):
    def new_function(arg): # 仅适用于旧函数具有一个参数的情况
        return old_function(arg * 2) # 修改传递的参数
    return new_function

并进行检查。

def check(old_function):
    def new_function(arg):
        if arg < 0: raise (ValueError, "Negative Argument") # 这会引发错误,比执行错误操作更好
        old_function(arg)
    return new_function

假设您想用一个可变的量来乘以输出。您可以定义装饰器并如下使用:

def multiply(multiplier):
    def multiply_generator(old_function):
        def new_function(*args, **kwds):
            return multiplier * old_function(*args, **kwds)
        return new_function
    return multiply_generator # 返回新的生成器

# 用法
@multiply(3) # multiply 不是生成器,但 multiply(3) 是
def return_num(num):
    return num

# 现在 return_num 被装饰并重新赋值给自身
return_num(5) # 应返回 15

您可以对旧函数做任何事情,甚至完全忽略它!高级装饰器还可以操作文档字符串和参数数量。 要获取一些炫酷的装饰器,请访问 http://wiki.python.org/moin/PythonDecoratorLibrary

Exercise

创建一个装饰器工厂,它返回一个装饰函数为单参数的装饰器。工厂应接收一个参数,一个类型,然后返回一个装饰器,该装饰器应检查输入是否是正确的类型。如果类型错误,则应打印("Bad Type")(实际上,应引发一个错误,但本教程中不包括错误引发)。如果感到困惑,请查看教程代码和期望输出(我肯定会这样的)。使用 isinstance(object, type_of_object) 或 type(object) 可能会有所帮助。 ```

def type_check(correct_type): #put code here @type_check(int) def times2(num): return num*2 print(times2(2)) times2('Not A Number') @type_check(str) def first_letter(word): return word[0] print(first_letter('Hello World')) first_letter(['Not', 'A', 'String']) def type_check(correct_type): def check(old_function): def new_function(arg): if (isinstance(arg, correct_type)): return old_function(arg) else: print("Bad Type") return new_function return check @type_check(int) def times2(num): return num*2 print(times2(2)) times2('Not A Number') @type_check(str) def first_letter(word): return word[0] print(first_letter('Hello World')) first_letter(['Not', 'A', 'String']) test_output_contains("4") test_output_contains("Bad Type") test_output_contains("H") test_output_contains("Bad Type") success_msg("Good job!")

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Previous Tutorial Next Tutorial Take the Test
Copyright © learnpython.org. Read our Terms of Use and Privacy Policy