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

1. @classmethod和@staticmethod

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

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

  1. class A():
  2. @classmethod
  3. def classM(cls):
  4. print "class method, and invoker:",cls.__name__
  5. @staticmethod
  6. def staticM():
  7. print "static method"
  8. class B(A):
  9. pass
  10.  
  11. A.classM() #class method, and invoker: A
  12. B.classM() #class method, and invoker: B
  13. A.staticM() #static method
  14. B.staticM() #static method
  15. a=A()
  16. a.classM() #class method, and invoker: A
  17. a.staticM() #static method
  18. b=B()
  19. b.classM() #class method, and invoker: B
  20. b.staticM() #static method

 

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

  1. class C():
  2. def func(fn):
  3. def test(*args):
  4. print "hello"
  5. return test
  6. @func
  7. def testone(a,b):
  8. print a**2+b**2
  9. if __name__=="__main__":
  10. testone(3,4)
  11.  
  12. #output:hello
  1. class C():
  2. def func(fn):
  3. def test(*args):
  4. print "hello"
  5. fn(*args)
  6. return test
  7. @func
  8. def testone(a,b):
  9. print a**2+b**2
  10. if __name__=="__main__":
  11. testone(3,4)
  12.  
  13. #output:
  14. hello
  15. 25

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

  1. def singleton(cls):
  2. instance={}
  3. def getinstance():
  4. if cls not in instance:
  5. instance[cls]=cls()
  6. return instance[cls]
  7. return getinstance
  8.  
  9. @singleton
  10. class Myclass:
  11. pass
  12.  
  13. #output
  14. >>> my1=Myclass()
  15. >>> print my1
  16. <__main__.Myclass instance at 0x00000000028C2F48>
  17. >>> my2=Myclass()
  18. >>> print my2
  19. <__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. 每天收获一点点------Hadoop之初始MapReduce

    一.神马是高大上的MapReduce MapReduce是Google的一项重要技术,它首先是一个编程模型,用以进行大数据量的计算.对于大数据量的计算,通常采用的处理手法就是并行计算.但对许多开发者来 ...

  2. Codeforces 450 C. Jzzhu and Chocolate

    //area=(n*m)/ ((x+1)*(k-x+1)) //1: x==0; //2: x=n-1 //3: x=m-1 # include <stdio.h> long long m ...

  3. 添加和删除行的能力table(能够编辑的表的内容)

    页面文件 <html> <head> <meta http-equiv="Content-Type" content="text/html; ...

  4. Centos7系统配置上的变化(二)网络管理基础

    原文 Centos7系统配置上的变化(二)网络管理基础 上篇简单介绍了CentOS 7 在服务和网络方面的一点变化,先前很多烂熟于心的操作指令已经不适用了,不管是否习惯,总要接受.熟悉这些变化. 写上 ...

  5. 持续集成并不能消除 Bug,而是让它们非常容易发现和改正(转)

    互联网软件的开发和发布,已经形成了一套标准流程,最重要的组成部分就是持续集成(Continuous integration,简称 CI). 本文简要介绍持续集成的概念和做法. 一.概念 持续集成指的是 ...

  6. 一个非常不错的gridview 风格

    <style type="text/css"> <!-- .datable {background-color: #9FD6FF; color:#333333;  ...

  7. poj 2449 Remmarguts&#39; Date 【SPFA+Astar】【古典】

    称号:poj 2449 Remmarguts' Date 意甲冠军:给定一个图,乞讨k短路. 算法:SPFA求最短路 + AStar 以下引用大牛的分析: 首先,为了说话方便,列出一些术语: 在启示式 ...

  8. matlab配置Libsvm 防止备忘录

    1 首先我们要下载一个Libsvm 工具箱 其中,这一切都可以被下载到 2 我们解包 我解压在桌面上 住址C:\Users\Administrator\Desktop\libsvm 3打开matlab ...

  9. 运用TWaver 3D 矢量图形处理能力

    的确,提起TWaver,大家想到的首先是"电信拓扑图组件".事实上.因为其灵活的MVC架构.矢量化设计.方便定制等特点.TWaver能够做的还有非常多.比如房地产行业常见到的&qu ...

  10. jQuery插件编写及链式编程模型

    jQuery插件编写及链式编程模型小结 JQuery极大的提高了我们编写JavaScript的效率,让我们可以愉快的编写代码,做出各种特效.大多数情况下,我们都是使用别人开发的JQuery插件,今天我 ...