Python中staticmethod和classmethod的差别
Class vs static methods in Python这篇文章试图表明:什么是staticmethod/classmethod,而且这两者之间的差别.
staticmethod和classmethod均被作为装饰器,用作界说一个函数为”staticmethod”照旧”classmethod”
假如想要相识Python装饰器的底子,可以看 这篇文章(http://www.pythoncentral.io/python-decorators-overview/)
Simple, static and class methods
类中最常用到的方法是 实例方法(instance methods), 即,实例对象作为第一个参数通报给函数
比方,下面是一个根本的实例方法
class Kls(object):
def __init__(self, data):
self.data = data
def printd(self):
print(self.data)
ik1 = Kls('arun')
ik2 = Kls('seema')
ik1.printd()
ik2.printd()
得到的输出:
arun
seema
调用关系图:
检察代码和图解:
1/2 参数通报给函数
3 self参数指向实例自己
4 我们不必要显式提供实例,表明器自己会处置惩罚
如果我们想仅实现类之间交互而不是通过实例?我们可以在类之外创建一个简朴的函数来实现这个功能,但是将会使代码扩散到类之外,这个大概对将来代码维护带来题目。
比方:
def get_no_of_instances(cls_obj):
return cls_obj.no_inst
class Kls(object):
no_inst = 0
def __init__(self):
Kls.no_inst = Kls.no_inst + 1
ik1 = Kls()
ik2 = Kls()
print(get_no_of_instances(Kls))
效果:
2
The Python @classmethod
如今我们要做的是在类里创建一个函数,这个函数参数是类对象而不是实例对象.
在上面谁人实现中,假如要实现不获取实例,必要修改如下:
def iget_no_of_instance(ins_obj):
return ins_obj.__class__.no_inst
class Kls(object):
no_inst = 0
def __init__(self):
Kls.no_inst = Kls.no_inst + 1
ik1 = Kls()
ik2 = Kls()
print iget_no_of_instance(ik1)
效果
2
可以利用Python2.2引入的新特性,利用@classmethod在类代码中创建一个函数
class Kls(object):
no_inst = 0
def __init__(self):
Kls.no_inst = Kls.no_inst + 1
@classmethod
def get_no_of_instance(cls_obj):
return cls_obj.no_inst
ik1 = Kls()
ik2 = Kls()
print ik1.get_no_of_instance()
print Kls.get_no_of_instance()
We get the following output:
2
2
The Python @staticmethod
通常,有许多环境下一些函数与类相干,但不必要任何类或实例变量就可以实现一些功能.
好比设置情况变量,修改另一个类的属性等等.这种环境下,我们也可以利用一个函数,一样会将代码扩散到类之外(难以维护)
下面是一个例子:
IND = 'ON'
def checkind():
return (IND == 'ON')
class Kls(object):
def __init__(self,data):
self.data = data
def do_reset(self):
if checkind():
print('Reset done for:', self.data)
def set_db(self):
if checkind():
self.db = 'new db connection'
print('DB connection made for:',self.data)
ik1 = Kls(12)
ik1.do_reset()
ik1.set_db()
效果:
Reset done for: 12
DB connection made for: 12
如今我们利用@staticmethod, 我们可以将全部代码放到类中
IND = 'ON'
class Kls(object):
def __init__(self, data):
self.data = data
@staticmethod
def checkind():
return (IND == 'ON')
def do_reset(self):
if self.checkind():
print('Reset done for:', self.data)
def set_db(self):
if self.checkind():
self.db = 'New db connection'
print('DB connection made for: ', self.data)
ik1 = Kls(12)
ik1.do_reset()
ik1.set_db()
得到的效果:
Reset done for: 12
DB connection made for: 12
How @staticmethod and @classmethod are different
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)
调用
>>> ik = Kls(23)
>>> ik.printd()
23
>>> ik.smethod()
Static: ()
>>> ik.cmethod()
Class: (,)
>>> Kls.printd()
TypeError: unbound method printd() must be called with Kls instance as first argument (got nothing instead)
>>> Kls.smethod()
Static: ()
>>> Kls.cmethod()
Class: (,)
图解
论坛有你更精彩! 真是难得给力的帖子啊。 党的好公民,人民的好公仆。。。 论坛有你更精彩! 淡定,淡定,淡定…… 我顶,我顶,我顶顶顶 加油,加油,不要沉下去,我是最热贴 楼主您的技术水准,我最服你,其他都是浮云 激动人心,无法言表!