理解python:@classmethod 和@staticmethod
2016-11-29 18:42
387 查看
具体详情,请看参考链接!
@classmethod:
使用地方:
和类进行交互,但不和其实例进行交互的函数方法
举个例子:
@staticmethod:
使用地方:
有一些和类相关函数,但不要使用该类或者该类的实例;比如更改环境变量或者修改其他类的属性等。
示例二:
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/
用途:
写与类进行交互的方法,而不是实例。@classmethod:
使用地方:
和类进行交互,但不和其实例进行交互的函数方法
举个例子:
在类外定义一个函数,来与类进行交互[坏处:扩散类代码的关系到类定义外]
class ClassandStaticMethod(object): value = "get the value without instance" def __init__(self,data): self.data = data def 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 successfuly staticmethod added successfuly get the value without instance added classmethod successfuly staticmethod_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/
相关文章推荐
- 新手理解@classmethod及@staticmethod
- Python 实例方法、@staticmethod和@classmethod
- python中@staticmethod 、@classmethod 与一般方法的区别
- python的@classmethod和@staticmethod
- Python中classmethod与staticmethod区别
- Python @classmethod 和 @staticmethod的比较
- Python 基础 —— @classmethod 与 @staticmethod 的意义及用途
- python @staticmethod和@classmethod的作用与区别
- python staticmethod和classmethod(转载)
- python staticmethod和classmethod
- Python学习笔记 --- @staticmethod 和 @classmethod方法的使用
- Python中@staticmethod和@classmethod的区别和用法
- python的静态方法以及@staticmethod或@classmethod的区别
- python - @staticmethod和@classmethod的作用与区别
- python staticmethod & classmethod
- Python 实例方法、@staticmethod和@classmethod
- python的 @staticmethod,@classmethod和@property的区别
- python 类中@staticmethod,@classmethod和实例方法
- Python 中的 @staticmethod 和 @classmethod
- python @property,@staticmethod,@classmethod 使用例子