python @修饰符_python中的 @ 修饰符

今天看到Python中的一个修饰符'@',不了解它的使用,查看了下官方文档,有了一点了解。

不得不佩服老外,治学很严谨,在python网站相关网页上把为什么使用decorator(主要为了简便一些代码),以及使用什么字符,甚至语法怎么设计写了个详详细细,好长的一篇啊。

这是查看的其中一篇,我翻译关键部分的一些内容,又摘取一些有用的,有空再翻译。

@dec2

@dec1deffunc(arg1, arg2, ...):pass

This is equivalent to(等价于):

deffunc(arg1, arg2, ...):passfunc= dec2(dec1(func))

使用示例:

Much of the discussion on comp.lang.pythonand the python-devmailing list focuses on the use of decorators as a cleaner way to use the staticmethod()and classmethod()builtins. This capability is much more powerful than that. This section presents some examples of use.

在comp.lang.python和 python-dev的大部分讨论集中在更简捷地使用内置修饰符staticmethod()和 classmethod()上。但修饰符的功能远比这强大。下面会对它的使用进行一些讲解:

1.Define a function to be executed at exit. Note that the function isn't actually "wrapped" in the usual sense.

1.定义一个执行即退出的函数。注意,这个函数并不像通常情况那样,被真正包裹。

defonexit(f):importatexit

atexit.register(f)returnf

@onexitdeffunc():

...

Note that this example is probably not suitable for real usage, but is for example purposes only.

注意,这个示例可能并不能准确表达在实际中的使用,它只是做一个示例。

2. Define a class with a singleton instance. Note that once the class

disappears enterprising programmers would have to be more creative to

create more instances. (From Shane Hathaway onpython-dev.)

2.定义一个只能产生一个实例的类(有实例后,这个类不能再产生新的实例)。注意,一旦这个类失效了(估计意思是保存在下文的singleton中字典中的相应键失效),就会促使程序员让这个类产生更多的实例。(来自于python-dev的Shane

Hathaway)

defsingleton(cls):

instances={}defgetinstance():if cls not ininstances:

instances[cls]=cls()returninstances[cls]returngetinstance

@singletonclassMyClass:

...

余下基本可以参照着读懂了,以后再翻译。 3.Add attributes to a function. (Based on an example posted by Anders Munch on python-dev.)

def attrs(**kwds):defdecorate(f):for k inkwds:

setattr(f, k, kwds[k])returnfreturndecorate

@attrs(versionadded="2.2",

author="Guido van Rossum")defmymethod(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):defcheck_accepts(f):assert len(types) ==f.func_code.co_argcountdef new_f(*args, **kwds):for (a, t) inzip(args, types):assertisinstance(a, t), \"arg %r does not match %s" %(a,t)return f(*args, **kwds)

new_f.func_name=f.func_namereturnnew_freturncheck_acceptsdefreturns(rtype):defcheck_returns(f):def new_f(*args, **kwds):

result= f(*args, **kwds)assertisinstance(result, rtype), \"return value %r does not match %s" %(result,rtype)returnresult

new_f.func_name=f.func_namereturnnew_freturncheck_returns

@accepts(int, (int,float))

@returns((int,float))deffunc(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-devbased on experience

with PyProtocols

def provides(*interfaces):"""An actual, working, implementation of provides for

the current implementation of PyProtocols. Not

particularly important for the PEP text."""

defprovides(typ):

declareImplementation(typ, instancesProvide=interfaces)returntypreturnprovidesclassIBar(Interface):"""Declare something about IBar here"""@provides(IBar)classFoo(object):"""Implement something here..."""

Of course, all these examples are possible today, though without syntactic support.

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值