Python一直都属于用,没有去系统学习过,在一次代码review中见到了@符号,回来看了下,这个符号用于装饰器中,用于修饰一个函数,把被修饰的函数作为参数传递给装饰器,下面举几个例子:

1. @classmethod和@staticmethod

这两个含义很明显,在定义方法的时候@classmethod表示该方法是类方法,类方法必须有一个参数为cls,表示类本身,实例方法的第一个参数是self.@staticmethod修饰的方法基本上和一个全局函数相同。

这两个修饰的方法通过实例和类调用都是可以的

class A():
@classmethod
def classM(cls):
print "class method, and invoker:",cls.__name__
@staticmethod
def staticM():
print "static method"
class B(A):
pass A.classM() #class method, and invoker: A
B.classM() #class method, and invoker: B
A.staticM() #static method
B.staticM() #static method
a=A()
a.classM() #class method, and invoker: A
a.staticM() #static method
b=B()
b.classM() #class method, and invoker: B
b.staticM() #static method

 

2. 作为普通的修饰符,下面的定义类似于 testone=func(testone)

class C():
def func(fn):
def test(*args):
print "hello"
return test
@func
def testone(a,b):
print a**2+b**2
if __name__=="__main__":
testone(3,4) #output:hello
class C():
def func(fn):
def test(*args):
print "hello"
fn(*args)
return test
@func
def testone(a,b):
print a**2+b**2
if __name__=="__main__":
testone(3,4) #output:
hello
25

3. 不常见的写法,用来修饰一个class,在单例模式中能用到

def singleton(cls):
instance={}
def getinstance():
if cls not in instance:
instance[cls]=cls()
return instance[cls]
return getinstance @singleton
class Myclass:
pass #output
>>> my1=Myclass()
>>> print my1
<__main__.Myclass instance at 0x00000000028C2F48>
>>> my2=Myclass()
>>> print my2
<__main__.Myclass instance at 0x00000000028C2F48>

  

  

Python的@符号的更多相关文章

  1. Python 输出格式符号

    Python 常见的输出格式符号

  2. Python的符号、对齐和用0填充

    # 用0填充 print("用0填充:{0:010.2f}".format(math.pi)) # 用1填充(事实上,你无法实现“用1填充”,因为即使实现了,那也是另外一个数字) ...

  3. Python字符串符号:双引号/单引号用法注解。

    众所周知python中单引号和双引号常常被我们所使用,例如print.input等等. 但是对于打印输出所引导的字符串大多都是用双引号的形式来做,"Hello,python!",而 ...

  4. Python 集合符号

    & 求交集 l 求并集 ^ 交叉补集 - 求差集 > = < =

  5. python运算符号

    运算符 比较运算 赋值运算 逻辑运算 成员运算

  6. python 正则表达式 符号及其定义

    较好的文章https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html

  7. python读写符号的含义

    r 打开只读文件,该文件必须存在. r+ 打开可读写的文件,该文件必须存在. w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失.若文件不存在则建立该文件. w+ 打开可读写文件,若文件 ...

  8. 【Python基础】lpthw - Exercise 37 复习各种符号

    本节需要熟悉python的符号和关键字的功能. 一.关键字 1. and 逻辑与,如 True and False == False的值为True 2. as with...as...的功能类似try ...

  9. python面试大全

    问题一:以下的代码的输出将是什么? 说出你的答案并解释. class Parent(object): x = 1 class Child1(Parent): pass class Child2(Par ...

随机推荐

  1. Extjs 4.2 右键菜单树节点(,选择逆,废除)

    写自己的最新版本号extjs4.2树节点的操作,记录它,可能在将来被用于. var tree = new Ext.tree.TreePanel({ flex: 1, animate: true, au ...

  2. 华为-on练习--重复的字符过滤

    称号: 请写一个字符串过滤程序,如果使用多个相同的字符出现在字符串中,字符首次出现在非过滤,. 比方字符串"abacacde"过滤结果为"abcde". 演示样 ...

  3. 照片总结---选择适当的NoSQL

    版权声明:本文博客原创文章,博客,未经同意,不得转载.

  4. BZOJ 1801 AHOI2009 中国象棋 递归

    标题效果:给定一个棋盘.放置一些枪.它需要随机两支枪不能互相攻击,评估的数目p模值 首先,两炮不攻击对方自由地等同于一条线最多可有只有两个枪 直形压力DP话是50分 考虑到每个列是等效 然后我们就可以 ...

  5. docker study

    Author:Hyphen 同步公布在个人博客中:http://yanheven.github.io/docker-study/ 參考章宇兄的开源项目学习ABC的方法来对docker进行简单的学习与分 ...

  6. MySQL汇总数据

    汇总数据 有时,数据本身是不上台面的操作数据表.但在摘要表中的数据.例如 数据的一列的平均值.极大值.至少值等一下. 对于这些频繁使用的数据的处理的概要,MySQL它提供了一个函数来处理. SQL聚集 ...

  7. oracle_恢复流程图

    网上看到一个很好的数据恢复检查图片,共享给大家

  8. AngularJS 疑难问题解决汇总

    AngularJS 防止页面闪烁的方法 angularjs filter 详解 学习资料1 学习资料2 在 AngularJS 应用中处理单选框和复选框 学习资料3 AngularJS 之 Facto ...

  9. ReactJS.NET

    初探ReactJS.NET 开发 ReactJS通常也被称为"React",是一个刚刚在这场游戏中登场的新手.它由Facebook创建,并在2013年首次发布.Facebook认为 ...

  10. UVA - 11986 Save from Radiation

    Description J Save from Radiation Most of you are aware of Nuclear Power Plant Explosion at Fukushim ...