python wrapper是什么

1,123次阅读
没有评论

python

python wrapper是什么

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

python wrapper应用场景

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

python wrapper应用实例

无参数的装饰器。

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中还是比较容易遇到装饰器的使用,可以在看完内容后做一些练习。

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

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-04-29发表,共计483字。
新手QQ群:570568346,欢迎进群讨论 Python51学习