首页 / PYTHON / Python基础 装饰器
Python基础 装饰器
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python基础 装饰器,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2770字,纯文字阅读大概需要4分钟。
内容图文
![Python基础 装饰器](/upload/InfoBanner/zyjiaocheng/814/6c6569d607c2450aad33fe1c7deaf358.jpg)
装饰器(decorator)
- 函数基础
# function.py 函数 # 1)函数是对象 def hello(): return 'Hello, world!' func = hello print(func) # <object> print(func()) # 2)函数可以嵌套,定义在另一个函数内部 def show(): print('Run in show()') def message(word='Hello'): return word print(message()) show() # 3)函数作为参数返回 def getWordType(kind=''): def default(word=''): return word def upper(word=''): return word.upper() def lower(word=''): return word.lower() if kind.lower() == 'lower': return lower elif kind.lower() == 'upper': return upper else: return default wordType = getWordType('upper') print(wordType) print(wordType('Resistance is futile!')) # 4)函数作为参数传入 def getName(name='leo'): return name def foo(func): print('I will call the getName function later') print(func()) foo(getName)
- 装饰器以及函数比较
# decorator.py 装饰器 # 不使用装饰器情况 def my_new_decorator(a_function_to_decorator): def the_wrapper_around_the_original_function(): print('Befor the function runs') a_function_to_decorator() print('After the function runs') return the_wrapper_around_the_original_function # 将函数作为参数返回 def a_stand_alone_function(): print("I'm a stand alone function, don't you dare modify me.") a_stand_alone_function() a_stand_alone_function_decorated = my_new_decorator(a_stand_alone_function) # 将函数作为参数传入 print(a_stand_alone_function_decorated) a_stand_alone_function_decorated() # 使用装饰器 语法糖"@" @my_new_decorator def another_stand_alone_function(): print('leave me alone') another_stand_alone_function() # same as: another_stand_alone_function = my_new_decorator(another_stand_alone_function)
- 装饰器应用
# decorator_web.py web中的装饰器应用 def makebold(func): def wrapper(): return '<b>' + func() + '</b>' return wrapper def makeitalic(func): def wrapper(): return '<i>' + func() + '</i>' return wrapper @makebold @makeitalic def word(): return 'Hello, decorator!' print(word())
# decorator_time.py 使用装饰器检查函数的运行时间 import time import os def timer(func): def wrapper(cwd): time_begin = time.ctime() print('Begin @' + time_begin) func(cwd) time_end = time.ctime() print('End @' + time_end) print('Begin @' + time_begin) print('End @' + time_end) return wrapper @timer def walk(cwd=r'D:\PyCharm\python\practice'): for root, dirs, files in os.walk(cwd): # print(root) # 打印当前目录下的所有子目录的绝对路径 # print(dirs) # 打印每个目录下的子目录列表 # print(files) # 打印所有目录下文件列表 print(root) for file in files: print('\t' + file) walk(cwd=r'D:\PyCharm')
- 包含所有参数以及带返回值的装饰器
# decorator_args.py 包含所有参数的装饰器 import time def timer(func): def inner(*args, **kwargs): start = time.time() re = func(*args, **kwargs) end = time.time() return re return inner @timer #==> func1 = timer(func1) def func1(a, b): print('in func1') return a, b @timer #==> func2 = timer(func2) def func2(a): print('In func2 and get', a) return 'func2 over' func1('aaaaaa', 'bbbbbb') print(func2('aaaaaa'))
内容总结
以上是互联网集市为您收集整理的Python基础 装饰器全部内容,希望文章能够帮你解决Python基础 装饰器所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。