理解python:@classmethod 和@staticmethod
来源:互联网 发布:控制网络的技术特点 编辑:程序博客网 时间:2024/05/29 04:54
具体详情,请看参考链接!
用途:
写与类进行交互的方法,而不是实例。
@classmethod:
使用地方:
和类进行交互,但不和其实例进行交互的函数方法
举个例子:
在类外定义一个函数,来与类进行交互[坏处:扩散类代码的关系到类定义外]
class ClassandStaticMethod(object): value = "get the value without instance" def __init__(self,data): self.data = datadef InteractClassWithoutInstance(cls): return cls.value+" added successfuly"print InteractClassWithoutInstance(ClassandStaticMethod)#result>get the value without instanceadded successfuly
使用@classmethod的方式:
class ClassandStaticMethod(object): value = "get the value without instance" def __init__(self,data): self.data = data @classmethod def InteractClassWithoutInstance(cls): return cls.value+" added successfuly"print ClassandStaticMethod.InteractClassWithoutInstance()#instance #也可以实例化之后使用该函数。cs = ClassandStaticMethod("Jack")print cs.InteractClassWithoutInstance()#result> get the value without instance added successfuly get the value without instance added successfuly
@staticmethod:
使用地方:
有一些和类相关函数,但不要使用该类或者该类的实例;比如更改环境变量或者修改其他类的属性等。
class ClassandStaticMethod(object): value = "get the value without instance" def __init__(self,data): self.data = data @classmethod def InteractClassWithoutInstance(cls): return cls.value+" added classmethod successfuly" @staticmethod def set_variable(value): return value+" added successfuly "print ClassandStaticMethod.InteractClassWithoutInstance()print ClassandStaticMethod.set_variable("staticmethod")#instance cs = ClassandStaticMethod("Jack")print cs.InteractClassWithoutInstance()print cs.set_variable("staticmethod_2")#result>get the value without instance added classmethod successfulystaticmethod added successfuly get the value without instance added classmethod successfulystaticmethod_2 added successfuly
示例二:
class Kls(object): def __init__(self, data): self.data = data def printd(self): print(self.data) @staticmethod def smethod(*arg): print('Static:', arg) @classmethod def cmethod(*arg): print('Class:', arg)Kls.smethod(1,2,4)Kls.cmethod(1,2,5)ik = Kls(123)ik.smethod(1,2,4)ik.cmethod(1,2,5)#result>('Static:', (1, 2, 4))('Class:', (<class '__main__.Kls'>, 1, 2, 5))('Static:', (1, 2, 4))('Class:', (<class '__main__.Kls'>, 1, 2, 5))
notice: classmethod需要一个对类的引用作为第一个参数,而staticmethod不需要。
ref:
- http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod-for-beginner
- http://pythoncentral.io/difference-between-staticmethod-and-classmethod-in-python/
0 0
- 理解python:@classmethod 和@staticmethod
- python staticmethod和classmethod
- Python @staticmethod和@classmethod
- 【python】classmethod 和staticmethod
- python classmethod和staticmethod
- python的staticmethod和classmethod
- Python中的staticmethod和classmethod
- Python中的staticmethod和classmethod
- Python 中的 classmethod 和 staticmethod
- Python @classmethod和@staticmethod区别
- python 中staticmethod和classmethod
- Python 中的 classmethod 和 staticmethod
- Python 中的 @staticmethod 和 @classmethod
- python的@classmethod和@staticmethod
- 理解python的staticmethod与classmethod实现
- 理解python的staticmethod与classmethod实现
- [python相关]Python中的staticmethod和classmethod
- Python 实例方法、@staticmethod和@classmethod
- 尝鲜系列之手把手教你搭建Ambari2.0.0(在线环境)
- js与jQuery选择器
- 常用代码
- Tachyon(现名:Alluxio):Spark生态系统中的分布式内存文件系统
- Android上传文件到Web服务器,PHP接收文件(一)
- 理解python:@classmethod 和@staticmethod
- 使用Fiddler进行iOS APP的HTTP/HTTPS抓包
- 【框架基础】:Java反射机制详解(三)
- 笔记:oracle表设置主键自增长
- 文章标题
- 用Shell精简log文件(shell的字符串和文件操作)
- UVa 1349 Optimal Bus Route Design
- oracle中redo和undo的区别
- 超容易操作的git(一)