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. 具体分析Struts工作流程

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXV3ZW56aGU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  2. My97DatePicker日历控件日报、每周和每月的选择

    My97DatePicker日历控件日报.每周和每月的选择 1.设计源代码 <%@ page language="java" import="java.util.* ...

  3. Mencached使用

    Mencached使用小记 该文章简单记录一下在Windows平台下安装与配置Memcached的方法,Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载. ...

  4. 提高你的Java代码质量吧:推荐在复杂字符串操作中使用正则表达式

    一.分析  字符串的操作,诸如追加.合并.替换.倒序.分隔等,都是在编码过程中经常用到的,而且Java也提供了append.replace.reverse.split等方法来完成这些操作,它们使用起来 ...

  5. React.js再探(四)

    不知道看官们还记不记得上一节的内容,关于生命周期的.我们来个例子重温且练习一下. 传送门:http://www.cnblogs.com/galenyip/p/4574400.html 我们来实现一下时 ...

  6. ASP.NET MVC2.0 自定义filters

    今天大家共同学习下ASP.NET MVC2.0中自定义filters,这一节主要学习下ActionFilterAttribute, ActionFilterAttribute继承IActionFilt ...

  7. jQuery中的 return false, e.preventDefault(), e.stopPropagation()的区别

    e.stopPropagation()阻止事件冒泡 <html><head>     <title></title>     <script sr ...

  8. BIZTALK项目中WEB引用WEBSERVICES服务时候报错

    近期工作中须要完毕通过BIZTALK完毕调用WEBLOGIC公布的WebServices服务,环境搭建好后,打开VS开发工具新建一个BIZTALK项目,加入WEB引用将对方公布的地址拷贝上去,能够正常 ...

  9. 高效率的Shell

    1. 批量将Excel转为CSV文件 XLSX2CSV: https://github.com/dilshod/xlsx2csv sudo easy_install xlsx2csv #安装Xlsx2 ...

  10. canvas绘制贝塞尔曲线

    原文:canvas绘制贝塞尔曲线 1.绘制二次方贝塞尔曲线 quadraticCurveTo(cp1x,cp1y,x,y); 其中参数cp1x和cp1y是控制点的坐标,x和y是终点坐标 数学公式表示如 ...