Python: 无参数的函数装饰器
写带参数的函数装饰器最纠结的是需要包好多层,最外层是接收参数的函数,它返回一个接收函数的的函数。但这样有个问题是,最终包装出来的装饰器必须加()调用一下,即使没有参数也需要这样做,因为调用这个最外层函数才能返回里面装饰器(就是接收函数的函数)。以前一篇为例,可以这样改进:
def opt_arguments(func):
def meta_wrapper(*args, **kwargs):
if len(args) == 1 and callable(args[0]):
return func(args[0])
else:
def meta_func(inner_func):
return func(inner_func, *args, **kwargs)
return meta_func
return meta_wrapper @opt_arguments
def annotation(func, **annotations):
"""
A decorator to collect all named args to function.__namedargs__,
all anonymous args to function.__unnamedargs__,
decorator's args to function.__annotations__.
""" @functools.wraps(func)
def func_wrapper(*args, **kwargs):
argspec = inspect.getargspec(func)
namedargs = inspect.getcallargs(func, *args, **kwargs) # def foo(a, b=0, *c, **d): pass
# foo(1, b=2, c=3, d=4) will convert c=3 to namedargs.
unnamedargs = namedargs.pop(argspec.varargs, ())
namedargs.update(namedargs.pop(argspec.keywords, {})) func_wrapper.__namedargs__ = namedargs
func_wrapper.__unnamedargs__ = unnamedargs
func_wrapper.__annotations__ = annotations
func(*args, **kwargs) return func_wrapper
这样得到的annotation可以无参数使用:

Python: 无参数的函数装饰器的更多相关文章
- Python带参数的函数装饰器
# -*- coding: utf-8 -*- # author:baoshan # 带参数的函数装饰器 def say_hello(country): def wrapper(func): def ...
- python学习日记(函数--装饰器)
楔子 前提,我有一段代码(一个函数). import time def run_time(): time.sleep(0.1) print('我曾踏足山巅') 需求1:现在,我想计算这段代码的运行时间 ...
- Python学习第四十一天函数装饰器传参数的用法
在不改变函数的结构的基础,我们给函数加新的功能,用是函数装饰器,如果要给函数传递参数,那么应该怎么做呢 @timerdef test2(name,age): time.sleep(3) print(' ...
- python语言中的函数装饰器
装饰器 什么是装饰器? 装饰:给已有的对象(函数)添加新的功能 器:工具 在python中指具备某些功能的函数 装饰器:装饰器就是一个给其他函数增加功能的函数 一种设计原则: ...
- python带参数的类装饰器
# -*- coding: utf-8 -*- # author:baoshan # 带参数的类装饰器(和不带参数的类装饰器有很大的不同) # 类装饰器的实现,必须实现__call__和__init_ ...
- python之路(九)-函数装饰器
装饰器 某公司的基础业务平台如下: def f1(): print('这是f1业务平台') def f2(): print('这是f2业务平台') def f3(): print('这是f3业务平台' ...
- 流畅的python第七章函数装饰器和闭包学习记录
本章讨论的话题 python如何计算装饰器句法 python如何判断变量是不是局部的(通过函数内部是否给变量赋值过来判断是否是局部变量) 闭包存在的原因和工作原理(闭包是一种函数,它会保留定义函数时存 ...
- Python小白学习之函数装饰器
装饰器 2018-10-25 13:49:37 装饰器从字面意思就是用来装饰的,在函数可以理解为:在函数中,我们不想影响原来的函数功能,又想给函数添加新的功能,这时候我们就用到了装饰器. 一般函数操作 ...
- Python @函数装饰器及用法
1.函数装饰器的工作原理 函数装饰器的工作原理是怎样的呢?假设用 funA() 函数装饰器去装饰 funB() 函数,如下所示: #funA 作为装饰器函数 def funA(fn): #... fn ...
随机推荐
- Google Chrome: Make the Bookmarks Bar Display as Icons Only
By reducing your bookmarks to show only the icons, you can access more of them from the Bookmarks ba ...
- android和ubifs
原文地址: http://opendevkit.com/?e=37 1. ubifs号称性能比yaffs2 好,同时压缩可读写,文件系统image体较小同时可写,相当不错 2. ubifs制作 (1) ...
- Dynamic CRM 2013学习笔记(三十五)自定义审批流6 - 审批通过后,再审批 - 二次审批
最近有个特殊的需求,客户想做二次审批,就是审批通过后,再走一次审批流程.最开始一想,这还不简单,审批通过后,直接把状态改成draft就完了,后来一试,发现一堆问题,比如第一次审批完后,界面是不允许修改 ...
- 基于 IdentityServer3 实现 OAuth 2.0 授权服务【客户端模式(Client Credentials Grant)】
github:https://github.com/IdentityServer/IdentityServer3/ documentation:https://identityserver.githu ...
- ASP.NET WEB API 中的路由调试与执行过程跟踪
路由调试 RouteDebugger 是调试 ASP.NET MVC 路由的一个好的工具,在ASP.NET WEB API中相应的有 WebApiRouteDebugger ,Nuget安装 Inst ...
- Java经典类库-Guava中的函数式编程讲解
如果我要新建一个java的项目,那么有两个类库是必备的,一个是junit,另一个是Guava.选择junit,因为我喜欢TDD,喜欢自动化测试.而是用Guava,是因为我喜欢简洁的API.Guava提 ...
- Redis安装与日常使用
下载与安装 $ wget http://download.redis.io/releases/redis-3.0.3.tar.gz $ tar xzf redis-3.0.3.tar.gz $ cd ...
- AT&T ASSEMBLY FOR LINUX AND MAC (SYS_FORK)
Fork() in C: (sys_fork.c) #include <stdio.h> #include <stdlib.h> #include <unistd.h&g ...
- paip.python NameError name 'xxx' is not defined\
paip.python NameError name 'xxx' is not defined\ 导入一个另一个文件里面的函数的时候儿,出孪这个err #这个仅仅导入孪file...要使用里面的fun ...
- MF-800U
MF-800U 价格:200元左右 https://item.taobao.com/item.htm?spm=a230r.1.14.6.kfkqoY&id=4963072384&ns= ...