Day11

函数装饰器

一切皆对象

  • 在Python中,变量的本质都是对象,字符串变量是一个str类型的对象,而函数同样是一个类型为 function的变量。
def fun():
 def fun():
    print('函数fun')


fun()
#函数fun

#我们可以将一个函数赋值给一个变量
f = fun

f()
#函数fun

#它的本质是一个类型为function的对象
print(type(f))
#<class 'function'>

在函数中再定义一个函数

def fun1():
    print('这里是fun1')

    def fun2():
        print('这里是fun2')

    fun2()


fun1()
# 这里是fun1
# 这里是fun2

fun2()
# 这里调用fun2会报 NameError 的错误 

# 这里说明fun2只能在函数中的调用,不能在函数外被调用。

从函数中返回一个函数

  • 在上面例子中我们得知,嵌套函数(在函数中定义的函数)只能在定义它的函数体中被调用。那么有没有办法可以在函数的外部执行调用呢?
def fun1():
    def fun2():
        print('这里是fun2')
    #将嵌套函数作为函数的返回值
    return fun2


# 我们可以通过调用fun1函数,获得fun2函数的对象
f = fun1()

f()
# 这里是fun2
# fun1()() 与上述调用等同

将函数作为参数传入另一个函数

def fun1(fun):
    print('在执行fun函数前做些事情')
    # 调用传入的函数fun
    fun()


def fun2():
    print('fun2函数执行中')


# 将fun2函数作为参数传入fun1函数
fun1(fun2)
# 在执行fun函数前做些事情
# fun2函数执行中

编写第一个装饰器

# 定义一个装饰器
def decorator(fun):
    def wrapper():
        print("在执行 fun函数 前做一些事")
        fun()
        print("在执行 fun函数 后做一些事")
    # 返回被装饰后的 fun函数
    return wrapper


def hello():
    print('hello python')


hello()
# hello python

# 使用 decorator函数 装饰 hello函数
hello_with_decorator = decorator(hello)

hello_with_decorator()
# 在执行 fun函数 前做一些事
# hello python
# 在执行 fun函数 后做一些事
  • 糖语法:在上面的例子中,装饰器的使用过于繁琐,不利于阅读。为了更贴近装饰器的作用(在不改变函数体的前提下,对函数的功能进行扩展),我们通过糖语法来使用装饰器。
# 定义一个装饰器
def decorator(fun):
    def wrapper():
        print("在执行 fun函数 前做一些事")
        fun()
        print("在执行 fun函数 后做一些事")
    # 返回被装饰后的 fun函数
    return wrapper

# 糖语法
@decorator
def hello():
    print('hello python')

hello()
# 在执行 fun函数 前做一些事
# hello python
# 在执行 fun函数 后做一些事

装饰带参数的函数

  • 在上面的例子中装饰器所装饰的函数是没有参数的,而在大多数情况下函数是否有参数是不确定的,为了满足普适性,下面继续完善我们的装饰器。
# 定义一个装饰器
def decorator(fun):
    # 使用不定长参数
    def wrapper(*args, **kwargs):
        print("在执行 fun函数 前做一些事")
        fun(*args, **kwargs)
        print("在执行 fun函数 后做一些事")
    # 返回被装饰后的 fun函数
    return wrapper

# 糖语法
@decorator
def hello(content: str):
    print(content)


hello("hello python")
# 在执行 fun函数 前做一些事
# hello python
# 在执行 fun函数 后做一些事

带参数的装饰器

  • 在一些比较复杂的应用场景中,为了满足开发需求,装饰器需要传入一些参数,那如何定义带参数的装饰器呢?
# 带参数的装饰器
def tag(name: str='<a>'):
    def decorator(f):
        # 使用不定长参数
        def wrapper(*args, **kwargs):
            content = f(*args, **kwargs)
            return '{tag}{content}{tag}'.format(tag=name, content=content)
        # 返回被装饰后的 fun函数
        return wrapper
    return decorator


@tag('<a>')
def hello(content: str = 'hello python'):
    return content


print(hello("hello python"))
# <a>hello python<a>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 2,860评论 2 9
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,145评论 1 32
  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,270评论 0 4
  • 第3章 基本概念 3.1 语法 3.2 关键字和保留字 3.3 变量 3.4 数据类型 5种简单数据类型:Unde...
    RickCole阅读 5,162评论 0 21
  • 1:感谢这个天气,闷热闷热的,晚上都无法入睡,感谢房东早上起来说要装空调。结果晚上回来就已经装了一个房间的空调,因...
    李泽恩阅读 293评论 0 0