python中的 @ 修饰符
今天学习廖老师的python教程,碰到了修饰符'@',不太了解,查看了下官方文档。
简单的整理下:
@dec2
@dec1
def func(arg1, arg2, ...):
pass
等价于
def func(arg1, arg2, ...):
pass
func = dec2(dec1(func))
使用示例:
在comp.lang.python 和 python-dev的大部分讨论集中在更简捷地使用内置修饰符staticmethod() 和 classmethod() 上。但修饰符的功能远比这强大。下面会对它的使用进行一些讲解:
1.定义一个执行即退出的函数。注意,这个函数并不像通常情况那样,被真正包裹。
def onexit(f):
import atexit
atexit.register(f)
return f @onexit
def func():
...
(Note that this example is probably not suitable for real usage, but is for example purposes only.)
2.定义一个只能产生一个实例的类(有实例后,这个类不能再产生新的实例)。注意,一旦这个类失效了(估计意思是保存在下文的singleton中字典中的相应键失效),就会促使程序员让这个类产生更多的实例。(来自于python-dev的Shane Hathaway)
def singleton(cls):
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls()
return instances[cls]
return getinstance @singleton
class MyClass:
...
3.Add attributes to a function. (Based on an example posted by Anders Munch on python-dev.)
def attrs(**kwds):
def decorate(f):
for k in kwds:
setattr(f, k, kwds[k])
return f
return decorate @attrs(versionadded="2.2",
author="Guido van Rossum")
def mymethod(f):
...
4.Enforce function argument and return types. Note that this copies the func_name attribute from the old to the new function. func_name was made writable in Python 2.4a3:
def accepts(*types):
def check_accepts(f):
assert len(types) == f.func_code.co_argcount
def new_f(*args, **kwds):
for (a, t) in zip(args, types):
assert isinstance(a, t), \
"arg %r does not match %s" % (a,t)
return f(*args, **kwds)
new_f.func_name = f.func_name
return new_f
return check_accepts def returns(rtype):
def check_returns(f):
def new_f(*args, **kwds):
result = f(*args, **kwds)
assert isinstance(result, rtype), \
"return value %r does not match %s" % (result,rtype)
return result
new_f.func_name = f.func_name
return new_f
return check_returns @accepts(int, (int,float))
@returns((int,float))
def func(arg1, arg2):
return arg1 * arg2
5.Declare that a class implements a particular (set of) interface(s). This is from a posting by Bob Ippolito on python-dev based on experience with PyProtocols [27].
def provides(*interfaces):
"""
An actual, working, implementation of provides for
the current implementation of PyProtocols. Not
particularly important for the PEP text.
"""
def provides(typ):
declareImplementation(typ, instancesProvide=interfaces)
return typ
return provides class IBar(Interface):
"""Declare something about IBar here""" @provides(IBar)
class Foo(object):
"""Implement something here..."""
python中的 @ 修饰符的更多相关文章
- Python 中的@修饰符作用
在Python 2.4以上的的函数中偶尔会看到函数定义的上一行有@functionName的修饰,这一下这个语法细节,其实这有点像C语言带参数的宏操作,解释器读到这样的修饰之后,会先解析@后的内容,直 ...
- python中的修饰符@的作用
1.一层修饰符 1)简单版,编译即实现 在一个函数上面添加修饰符 @另一个函数名 的作用是将这个修饰符下面的函数作为该修饰符函数的参数传入,作用可以有比如你想要在函数前面添加记录时间的代码,这样每个函 ...
- JAVA语言中的修饰符
JAVA语言中的修饰符 -----------------------------------------------01--------------------------------------- ...
- Java中的 修饰符
java中的修饰符分为类修饰符,字段修饰符,方法修饰符. 根据功能的不同,主要分为以下几种. 1.权限访问修饰符 访问权限的控制常被称为具体实现的隐藏 把数据和方法包进类中,以及具体实现的隐藏,常共 ...
- C/C++ 中 const 修饰符用法总结
C/C++ 中 const 修饰符用法总结 在这篇文章中,我总结了一些C/C++语言中的 const 修饰符的常见用法,供大家参考. const 的用法,也是技术性面试中常见的基础问题,希望能够帮大家 ...
- Java中final修饰符深入研究
一.开篇 本博客来自:http://www.cnblogs.com/yuananyun/ final修饰符是Java中比较简单常用的修饰符,同时也是一个被"误解"较多的修饰符.对很 ...
- vue中的修饰符
Vue2.0学习笔记:Vue事件修饰符的使用 事件处理 如果需要在内联语句处理器中访问原生DOM事件.可以使用特殊变量$event,把它传入到methods中的方法中. 在Vue中,事件修饰符处理 ...
- Java中各种修饰符与访问修饰符
Java中各种修饰符与访问修饰符 类: 访问修饰符 修饰符 class 类名称 extends 父类名称 implement 接口名称 (访问修饰符与修饰符的位置可以互换) 访问修饰符 名称 说明 备 ...
- Java中访问修饰符public、private、protecte、default
Java中访问修饰符public.private.protecte.default的意义讲解:public: Java语言中访问限制最宽的修饰符,一般称之为“公共的”.被其修饰的类.属性以及方法不 仅 ...
随机推荐
- Linux命令 + Shell
1. 之前利用Ubuntu14.10的镜像安装了个虚拟机,本以为自己在windows上的就是管理员的权限,就理所当然的认为虚拟的Linux系统也是root权限.而且虽然@符号前的的标识不是root,但 ...
- 【JQuery NoviceToNinja系列】01 开篇 Html页面设计和布局
01 开篇 Html页面设计和布局 index.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml ...
- vs2010把项目资源打包成系统资源
把wav格式的音频做成系统资源,根据条件播放相应的音频 System.Media.SoundPlayer spOne = new System.Media.SoundPlayer(); ...
- LSTM/RNN的应用Case
作者:许铁-巡洋舰科技链接:https://www.zhihu.com/question/37082800/answer/126430702来源:知乎著作权归作者所有,转载请联系作者获得授权. 作者: ...
- ZOJ3229 Shoot the Bullet(有源汇的上下界最大流)
#pragma warning(disable:4996) #include <iostream> #include <cstring> #include <string ...
- POJ3164 Command Network(最小树形图)
图论填个小坑.以前就一直在想,无向图有最小生成树,那么有向图是不是也有最小生成树呢,想不到还真的有,叫做最小树形图,网上的介绍有很多,感觉下面这个博客介绍的靠谱点: http://www.cnblog ...
- LoaderManager使用详解(四)---实例:AppListLoader
实例:AppListLoader 这篇文章将是我的第四篇,也就是最后一篇该系列的文章.请在评论里面告诉我他们是否有用.前面几篇文章的链接如下: 一:Loaders之前世界 二:了解Loader ...
- Mysql统计总结 - 最近30天,昨天的数据统计
-- 最近30天的医说发布数量SELECT substr(a.feed_publish_time,6, 5) AS '日期', count(*) AS '医说数' FROM xm_feed a WHE ...
- zoj 2290 Game 博弈论
思路:HDU有过类似的题目,也就是谁面对FIB数,就处于必败状态. 再求第二问的时候要注意不一定要在一步之内就让对手处于必败状态,可以多步进行, 这个过程可以用递归实现. 代码如下: #include ...
- JS加载时间线
1.创建Document对象,开始解析web页面.解析HTML元素和他们的文本内容后添加Element对象和Text节点到文档中.这个阶段document.readyState = 'loading' ...