python 关于decorator
来源:互联网 发布:网络计算机培训 编辑:程序博客网 时间:2024/05/16 12:41
最近在学flask 的时候对于decorator 的一些疑惑:
from functools import wrapsfrom flask import abortfrom flask_login import current_userfrom app.models import Permissiondef permission_required(permission): def decorator(x): @wraps(x) def decorated_function(*args,**kwargs): if not current_user.can(permission): abort(403) return x(*args,**kwargs) return decorated_function return decoratordef admin_required(f): return permission_required(Permission.ADMINISTRATOR)(f)
在使用@permission_required 时,我在想,为什么看别人的例子需要给个func的参数??而我这个除了func 之外还有参数就不用写个func的形参??
import sysdebug_log = sys.stderrdef trace(func): if debug_log: def callf(*args, **kwargs): """A wrapper function.""" debug_log.write('Calling function: {}\n'.format(func.__name__)) res = func(*args, **kwargs) debug_log.write('Return value: {}\n'.format(res)) return res return callf else: return func@tracedef square(x): """Calculate the square of the given number.""" return x * xif __name__ == '__main__': print(square(3))
阅读全文
0 0
- python 关于decorator
- python decorator
- python decorator
- python decorator
- python decorator
- Python - Decorator
- Python decorator
- python decorator
- python decorator
- Python Decorator
- Python Decorator
- python decorator
- Python Decorator
- python decorator
- Python - decorator
- Python Decorator
- Python - Decorator
- python decorator
- 成长——新的开始,一切都是美好的
- 终于解决了QQ互联总是系统繁忙,请稍后再试的问题。内详!
- ZOJ-1259 输出所有的出站情况
- GitHub:pull最新内容
- 欢迎使用CSDN-markdown编辑器
- python 关于decorator
- java Integer与int的装箱(缓存)与拆箱
- LeetCode: 368. Largest Divisible Subset
- Controller控制器
- POJ 2299Ultra-QuickSort
- 我的学习之路_第九章_File类,递归
- ZOJ--1078:Palindrom Numbers
- Mysql主从复制原理解析和技术难点解决
- nginx 学习笔记(三)基本数据结构