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. HDU 3172 Virtual Friends(并用正确的设置检查)

    职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...

  2. 十天学Linux内核之第十天---总结篇(kconfig和Makefile & 讲不出再见)

    原文:十天学Linux内核之第十天---总结篇(kconfig和Makefile & 讲不出再见) 非常开心能够和大家一起分享这些,让我受益匪浅,感激之情也溢于言表,,code monkey的 ...

  3. Ubuntu设置交换空间參考教程[图]

    假设你当前使用的Ubuntu系统,不管是虚拟机还是实体机,没有交换分区或交换分区空间不足,能够为其设置交换空间. 本文提供的是一种设置交换空间的简单方法. 如若转载,请注明博文地址及原作者(Risin ...

  4. iOS学习笔记---简单的学习总结

    1.xcode6.0官方的版本必须是OS X10.9.4而以上的版本安装前: 2,xcode6.0正式版创建命令行项目时,无法选择swift语言:可是创建iOS应用项目时能够选择swift语言. 3, ...

  5. 《C++ Primer Plus》学习笔记6

    <C++ Primer Plus>学习笔记6 第11章 使用类 <<<<<<<<<<<<<<<&l ...

  6. android 环境使用smack 必须注冊的组件

    // Private Data Storage pm.addIQProvider("query", "jabber:iq:private",new Privat ...

  7. jquery datatables api (转)

    学习可参考:http://www.guoxk.com/node/jquery-datatables http://yuemeiqing2008-163-com.iteye.com/blog/20069 ...

  8. hdu 2141 Can you find it?(二分查找变例)

    Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now yo ...

  9. 【高德地图API】从零开始学高德JS API(八)——地址解析与逆地址解析

    原文:[高德地图API]从零开始学高德JS API(八)——地址解析与逆地址解析 摘要:无论是百度LBS开放平台,还是高德LBS开放平台,其调用量最高的接口,必然是定位,其次就是地址解析了,又称为地理 ...

  10. B二分法

    <span style="color:#330099;">/* B - 二分法 基金会 Time Limit:1000MS Memory Limit:65536KB 6 ...