今天学习廖老师的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中的 @ 修饰符的更多相关文章

  1. Python 中的@修饰符作用

    在Python 2.4以上的的函数中偶尔会看到函数定义的上一行有@functionName的修饰,这一下这个语法细节,其实这有点像C语言带参数的宏操作,解释器读到这样的修饰之后,会先解析@后的内容,直 ...

  2. python中的修饰符@的作用

    1.一层修饰符 1)简单版,编译即实现 在一个函数上面添加修饰符 @另一个函数名 的作用是将这个修饰符下面的函数作为该修饰符函数的参数传入,作用可以有比如你想要在函数前面添加记录时间的代码,这样每个函 ...

  3. JAVA语言中的修饰符

    JAVA语言中的修饰符 -----------------------------------------------01--------------------------------------- ...

  4. Java中的 修饰符

    java中的修饰符分为类修饰符,字段修饰符,方法修饰符. 根据功能的不同,主要分为以下几种. 1.权限访问修饰符  访问权限的控制常被称为具体实现的隐藏 把数据和方法包进类中,以及具体实现的隐藏,常共 ...

  5. C/C++ 中 const 修饰符用法总结

    C/C++ 中 const 修饰符用法总结 在这篇文章中,我总结了一些C/C++语言中的 const 修饰符的常见用法,供大家参考. const 的用法,也是技术性面试中常见的基础问题,希望能够帮大家 ...

  6. Java中final修饰符深入研究

    一.开篇 本博客来自:http://www.cnblogs.com/yuananyun/ final修饰符是Java中比较简单常用的修饰符,同时也是一个被"误解"较多的修饰符.对很 ...

  7. vue中的修饰符

    Vue2.0学习笔记:Vue事件修饰符的使用   事件处理 如果需要在内联语句处理器中访问原生DOM事件.可以使用特殊变量$event,把它传入到methods中的方法中. 在Vue中,事件修饰符处理 ...

  8. Java中各种修饰符与访问修饰符

    Java中各种修饰符与访问修饰符 类: 访问修饰符 修饰符 class 类名称 extends 父类名称 implement 接口名称 (访问修饰符与修饰符的位置可以互换) 访问修饰符 名称 说明 备 ...

  9. Java中访问修饰符public、private、protecte、default

    Java中访问修饰符public.private.protecte.default的意义讲解:public: Java语言中访问限制最宽的修饰符,一般称之为“公共的”.被其修饰的类.属性以及方法不 仅 ...

随机推荐

  1. PHP操作数据库类

    <?php /** * 功能: 数据库操作类 . * 作者: 赵铭哲 * 日期: 2016-05-23 * 时间: 9:43 */ namespace ZH\DataBase; use \Exc ...

  2. A trip through the graphics pipeline 2011 Part 10(翻译)

    之前的几篇翻译都烂尾了,这篇希望....能好些,恩,还有往昔呢. ------------------------------------------------------------- primi ...

  3. ActiveMQ主从配置

    这种方式有个问题,activemq1有消息没消费完但是突然宕机,虽然程序会自动连到activemq2.但是activemq1的消息只有等机器恢复后才会被消费. 1.启动:我这里使用的是apache-a ...

  4. JS模板引擎 :ArtTemplate (1)

    1.为什么需要用到模板引擎 我们在做前端开发的时候,有时候经常需要根据后端返回的json数据,然后来生成html,再显示到页面中去. 例如这样子: var data = [ {text: " ...

  5. Sqlite中使用rowid来表示行号,用于分页。

    在SQLite的查询结果中显示行号,可以使用select rowid as RowNumber ,* from WSCLanguage: select rowid as RowNumber ,* fr ...

  6. PHP soap Web Service 使用SoapDiscovery.class.php 生成wsdl文件

    PHP soap web service 使用wsdl文件 demo: ============================================================== 服 ...

  7. 山寨小小军团开发笔记 之 GamePool

    很多时候我们对于物体(比如弓箭)大量的生成与销毁,这个时候可以把弓箭放在内存池中进行管理,加快体验.自己Copy了一个简易版的. 一.代码 GameObjectPoolManager.cs using ...

  8. iptables使用multiport 添加多个不连续端口 不指定

    iptables使用multiport 添加多个不连续端口   碟舞飞扬 , 01:26 , Linux技术 , 评论(0) , 引用(0) , 阅读(12214) , Via 本站原创 大 | 中  ...

  9. Google Chrome 浏览器禁用缓存

    在使用 Google Chrome 浏览器调试 js 时,会发现修改完 js 不会立即生效,这是由于 chrome 浏览器缓存的原因,而在火狐下没有这个问题.经常使用 chrome 浏览器调试 js ...

  10. Ruby Profiler 详解之 stackprof

    简介 stackprof 是基于采样的一个调优工具,采样有什么好处呢?好处就是你可以线上使用,按照内置的算法抓取一部分数据,只影响一小部分性能.它会产生一系列的 dump 文件,然后你在线下分析这些文 ...