clsse。。。
类变量
1、需要在⼀个类的各个对象间交互,即需要⼀个数据对象为整个类⽽⾮某个对象服务。2、同时⼜⼒求不破坏类的封装性,即要求此成员隐藏在类的内部,对外不可见。3、有独⽴的存储区,属于整个类。
在python中是这样使⽤的:
class MyClass(): cls_count = 0
实例变量
1、当⼀个类实例化成特定对象后,在该对象命名空间的变量,只有该实例对象可以访问修改在python中是这样使⽤的:
class MyClass(): def __init__(self): self.count = 0
cls:python中约定成俗的代表类本⾝的变量
self:python中约定成俗的代表类实例本⾝的变量
classmethod
python中的语法糖,将⼀个⽅法定义在⼀个类的命令空间中,可以通过cls访问类变量,但不能访问实例变量
staticmethod
python中的语法糖,将⼀个⽅法定义在⼀个类的命名空间中,
和类⽅法差别不⼤,他们的主要差别是在类⽅法内部的时候,类⽅法可以有cls的类引⽤,静态访问则没有,如果静态⽅法想使⽤类变量,只能硬编码类名
下⾯是⼀个混合使⽤的例⼦,可以观察输出,判断他们之间的不同
class demo(): cls_count = 0
def __init__(self): self.count = 0
print self, ' instance is created'
@classmethod
def cls_add_class_count(cls):
print 'classmethod before: demo count is: ', cls.cls_count cls.cls_count = cls.cls_count + 1
print 'classmethod after: demo count is: ', cls.cls_count
@staticmethod
def static_add_class_count():
print 'staticmethod before: demo count is: ', demo.cls_count demo.cls_count = demo.cls_count + 1
print 'staticmethod after: demo count is: ', demo.cls_count def instance_add_class_count(self):
print 'instance befor: demo count is: ', self.cls_count self.cls_count = self.cls_count + 1
print 'instance after: demo count is: ', self.cls_count def instance_add_instance_count(self):
print 'instance befor: instance count is: ', self.count self.count = self.count + 1
print 'instance after: instance count is: ', self.count
if __name__ == '__main__': d1 = demo() d2 = demo()
print 'call classmethod' d1.cls_add_class_count() d2.cls_add_class_count() print demo.cls_count print '----------------'
print 'call staticmethod'
print 'staticmethod'
d1.static_add_class_count() d2.static_add_class_count() print demo.cls_count print '----------------'
print 'call instance add class count method' d1.instance_add_class_count() d2.instance_add_class_count() print '----------------'
print 'call instance add instance count method' d1.instance_add_instance_count() d2.instance_add_instance_count()
输出为:
<__main__.demo instance at 0x00000000022A9B48> instance is created<__main__.demo instance at 0x00000000022A9B88> instance is createdcall classmethod
classmethod before: demo count is: 0classmethod after: demo count is: 1classmethod before: demo count is: 1classmethod after: demo count is: 22
----------------call staticmethodstaticmethod
staticmethod before: demo count is: 2staticmethod after: demo count is: 3staticmethod before: demo count is: 3staticmethod after: demo count is: 44
----------------call instance add class count methodinstance befor: demo count is: 4instance after: demo count is: 5instance befor: demo count is: 4instance after: demo count is: 5----------------call instance add instance count methodinstance befor: instance count is: 0instance after: instance count is: 1instance befor: instance count is: 0instance after: instance count is: 1
总结
1 staticmethod,classmethod和实例⽅法都可以访问到类变量,但是,staticmethod通过类名访问,classmethod通过传⼊的cls访问,两者都直接通过类本⾝直接调⽤。⽽实例⽅法需要类实例化,先在实例变量中查找,如果找不到,再到类变量中访问。
2 也就是说实例⽅法可以实现staticmethod和classmethod的效果,但是使⽤staticmethod和classmethod可以从代码上直接说明该⽅法的作⽤
因篇幅问题不能全部显示,请点此查看更多更全内容