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 ...
随机推荐
- 83. 从视图索引说Notes数据库(上)
索引是数据库系统重要的feature,不管是传统的关系型数据库还是时兴的NoSQL数据库,它攸关查询性能,因而在设计数据库时须要细加考量.然而,Lotus Notes隐藏技术底层.以用户界面为导向.追 ...
- 数组排序、递归——(Java学习笔记二)
升序: 选择排序: 选定一个元素,一次和后面的元素相比较,如果选定的元素大雨后面的比较元素,就交换位置 先出现最小值,最后出现最大值. public stat ...
- docker study
Author:Hyphen 同步公布在个人博客中:http://yanheven.github.io/docker-study/ 參考章宇兄的开源项目学习ABC的方法来对docker进行简单的学习与分 ...
- Ubuntu Server 14.04 LTS(64bit)已安装 weblogic Server 12c(12.1.3) Zip Distribution
这里说的对Ubuntu Server 14.04 LTS(64bit)已安装weblogic Server 12c(12.1.3) Zip Distribution遇到的问题.至于Windows什么好 ...
- 《代码的第一行——Android》封面诞生
<代码的第一行--Android>已经上市近一个月,现在的情况是相当不错的销售,也特别感谢众多朋友的支持. 其实一本好书,假设你想卖.除了给予外力所要求的内容.封面设计是至关重要的,这本书 ...
- 词组缩写(isalpha()的应用)
Problem Description 定义:一个词组中每个单词的首字母的大写组合称为该词组的缩写.比如,C语言里常用的EOF就是end of file的缩写. Input 输入的第一行是一个整数 ...
- P31RestKit.dll 2.0 Unity3d json
using System.Collections.Generic; using UnityEngine; using System.Collections; using Prime31; public ...
- Android "QR二维码扫描"
支持灯 扫描结果 支持 抄.分享.浏览打开(超链接) 自己主动保存扫描记录 划删除 和源代码 git: http://git.oschina.net/892642257/QRCode csdn(0分) ...
- 第3章1节《MonkeyRunner源码剖析》脚本编写示例: MonkeyRunner API使用示例(原创)
天地会珠海分舵注:本来这一系列是准备出一本书的,详情请见早前博文“寻求合作伙伴编写<深入理解 MonkeyRunner>书籍“.但因为诸多原因,没有如愿.所以这里把草稿分享出来,所以错误在 ...
- 于CentOS 6 安装 Wordpress
1.两种方式给Wordpress 首先,你可以去wordpress最新的官方网站看看wordpress多少下载.例wordpress 3.9.1下载地址: http://cn.wordpress.or ...