Python的@符号
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的@符号的更多相关文章
- Python 输出格式符号
Python 常见的输出格式符号
- Python的符号、对齐和用0填充
# 用0填充 print("用0填充:{0:010.2f}".format(math.pi)) # 用1填充(事实上,你无法实现“用1填充”,因为即使实现了,那也是另外一个数字) ...
- Python字符串符号:双引号/单引号用法注解。
众所周知python中单引号和双引号常常被我们所使用,例如print.input等等. 但是对于打印输出所引导的字符串大多都是用双引号的形式来做,"Hello,python!",而 ...
- Python 集合符号
& 求交集 l 求并集 ^ 交叉补集 - 求差集 > = < =
- python运算符号
运算符 比较运算 赋值运算 逻辑运算 成员运算
- python 正则表达式 符号及其定义
较好的文章https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
- python读写符号的含义
r 打开只读文件,该文件必须存在. r+ 打开可读写的文件,该文件必须存在. w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失.若文件不存在则建立该文件. w+ 打开可读写文件,若文件 ...
- 【Python基础】lpthw - Exercise 37 复习各种符号
本节需要熟悉python的符号和关键字的功能. 一.关键字 1. and 逻辑与,如 True and False == False的值为True 2. as with...as...的功能类似try ...
- python面试大全
问题一:以下的代码的输出将是什么? 说出你的答案并解释. class Parent(object): x = 1 class Child1(Parent): pass class Child2(Par ...
随机推荐
- 每天收获一点点------Hadoop之初始MapReduce
一.神马是高大上的MapReduce MapReduce是Google的一项重要技术,它首先是一个编程模型,用以进行大数据量的计算.对于大数据量的计算,通常采用的处理手法就是并行计算.但对许多开发者来 ...
- 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 ...
- 添加和删除行的能力table(能够编辑的表的内容)
页面文件 <html> <head> <meta http-equiv="Content-Type" content="text/html; ...
- Centos7系统配置上的变化(二)网络管理基础
原文 Centos7系统配置上的变化(二)网络管理基础 上篇简单介绍了CentOS 7 在服务和网络方面的一点变化,先前很多烂熟于心的操作指令已经不适用了,不管是否习惯,总要接受.熟悉这些变化. 写上 ...
- 持续集成并不能消除 Bug,而是让它们非常容易发现和改正(转)
互联网软件的开发和发布,已经形成了一套标准流程,最重要的组成部分就是持续集成(Continuous integration,简称 CI). 本文简要介绍持续集成的概念和做法. 一.概念 持续集成指的是 ...
- 一个非常不错的gridview 风格
<style type="text/css"> <!-- .datable {background-color: #9FD6FF; color:#333333; ...
- poj 2449 Remmarguts' Date 【SPFA+Astar】【古典】
称号:poj 2449 Remmarguts' Date 意甲冠军:给定一个图,乞讨k短路. 算法:SPFA求最短路 + AStar 以下引用大牛的分析: 首先,为了说话方便,列出一些术语: 在启示式 ...
- matlab配置Libsvm 防止备忘录
1 首先我们要下载一个Libsvm 工具箱 其中,这一切都可以被下载到 2 我们解包 我解压在桌面上 住址C:\Users\Administrator\Desktop\libsvm 3打开matlab ...
- 运用TWaver 3D 矢量图形处理能力
的确,提起TWaver,大家想到的首先是"电信拓扑图组件".事实上.因为其灵活的MVC架构.矢量化设计.方便定制等特点.TWaver能够做的还有非常多.比如房地产行业常见到的&qu ...
- jQuery插件编写及链式编程模型
jQuery插件编写及链式编程模型小结 JQuery极大的提高了我们编写JavaScript的效率,让我们可以愉快的编写代码,做出各种特效.大多数情况下,我们都是使用别人开发的JQuery插件,今天我 ...