• 技术文章 >Python技术 >Python基础教程

    python wrapper是什么

    小妮浅浅小妮浅浅2021-03-18 10:47:04原创12138

    1、说明

    wrapper是装饰器的意思,装饰器本质上是一个Python函数。可以让其他函数,在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象。

    2、应用场景

    插入日志、性能测试、事务处理、缓存、权限校验等

    3、实例

    无参数的装饰器。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    def debug(func):

        def wrapper():

            print('[DEBUG]: enter {}()'.format(func.__name__))

            return func()

        return wrapper

      

    @debug

    def say_hello():

        print('hello!')

    say_hello()

    """

    [DEBUG]: enter say_hello()

    hello!

    """

    以上就是python wrapper的介绍,大家在python中还是比较容易遇到装饰器的使用,可以在看完内容后做一些练习。更多Python学习指路:python基础教程

    (推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)

    专题推荐:python wrapper
    上一篇:python gui是什么 下一篇:python try是什么意思

    相关文章推荐

    • python gil是什么• python自动化是什么• python idle 是什么• python truncate是什么• python os模块怎么用?• python sys模块是什么• python gui是什么

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网