类变量

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 created
call classmethod
classmethod before: demo count is: 0
classmethod after: demo count is: 1
classmethod before: demo count is: 1
classmethod after: demo count is: 2
2
----------------
call staticmethod
staticmethod
staticmethod before: demo count is: 2
staticmethod after: demo count is: 3
staticmethod before: demo count is: 3
staticmethod after: demo count is: 4
4
----------------
call instance add class count method
instance befor: demo count is: 4
instance after: demo count is: 5
instance befor: demo count is: 4
instance after: demo count is: 5
----------------
call instance add instance count method
instance befor: instance count is: 0
instance after: instance count is: 1
instance befor: instance count is: 0
instance after: instance count is: 1
 

总结

1 staticmethod,classmethod和实例方法都可以访问到类变量,但是,staticmethod通过类名访问,classmethod通过传入的cls访问,两者

都直接通过类本身直接调用。而实例方法需要类实例化,先在实例变量中查找,如果找不到,再到类变量中访问。

2 也就是说实例方法可以实现staticmethod和classmethod的效果,但是使用staticmethod和classmethod可以从代码上直接说明该方法的作用

Python staticmethod classmethod 普通方法 类变量 实例变量 cls self 概念与区别的更多相关文章

  1. Java——静态变量/方法与实例变量/方法的区别

    静态只能调用静态 非静态: 对象名.方法名 package ti; //通过两个类 StaticDemo.LX4_1 说明静态变量/方法与实例变量/方法的区别. class StaticDemo { ...

  2. python @staticmethod @classmethod self cls方法区别

    一直在用这些东西,但是又从来没有总结过,正好今日想起来就总结一下这些东西 @staticmethod 静态方法,名义上归属类管理,不能使用类变量和实例变量,类的工具包放在函数前,不能访问类属性和实例属 ...

  3. python 类中staticmethod,classmethod,普通方法

    1.staticmethod:静态方法和全局函数类似,但是通过类和对象调用. 2.classmethod:类方法和类相关的方法,第一个参数是class对象(不是实例对象).在python中class也 ...

  4. 第8.12节 Python类中使用__dict__定义实例变量和方法

    上节介绍了使用实例的__dict__查看实例的自定义属性,其实还可以直接使用__dict__定义实例变量和实例方法. 一. 使用__dict__定义实例变量 语法: 对象名. dict[属性名] = ...

  5. Python 类变量 实例变量

    类变量: ​ 是可在类的所有实例之间共享的值(也就是说,它们不是单独分配给每个实例的).例如下例中,num_of_instance 就是类变量,用于跟踪存在着多少个Test 的实例. 实例变量: 实例 ...

  6. staticmethod classmethod property方法

    @staticmethod 静态方法 函数修饰符,用来修饰一个函数,类似于装饰器 class Dog(object): def __init__(self,name): self.name = nam ...

  7. python_94_类变量实例变量

    class Role: n=123#类变量 name='我是类name' list=[] def __init__(self,name,role,weapon,life_value=100,money ...

  8. class_copyIvarList方法获取实例变量问题引发的思考

    在runtime.h中,你可以通过其中的一个方法来获取实例变量,那就是class_copyIvarList方法,具体的实现如下: - (NSArray *)ivarArray:(Class)cls { ...

  9. python子类如何继承父类的实例变量?

    类型1:父类和子类的实例变量均不需要传递 class A(object): def __init__(self): self.name = "cui" def get_name(s ...

随机推荐

  1. Java获取文件后缀名

    int dot = filename.lastIndexOf('.'); if ((dot > -1) && (dot < (licenceImg.getOriginalF ...

  2. Verilog中的阻塞与非阻塞

    这篇文档值得阅读 按说阻塞与非阻塞是Verilog中最基本的东西,也是老生常谈.但是最近看到很多程序里用到阻塞语句竟然不是很明白,说到底是从来没有自己仔细分析过.当然一般情况程序中也是推荐用非阻塞的. ...

  3. C++ char, unsigned char, signed char

    C语言中的 char, unsigned char, signed char 一.他们是什么? signed char是有符号的,但是unsigned char没有符号,两者在存储上没有任何区别都是8 ...

  4. Cmd Markdown语法参考

    https://www.zybuluo.com/mdeditor markdown语法说明 Markdown中公式的写法 $$P(X=k)=C_n^kp^k(1-p)^{n-k}$$ 欢迎使用 Cmd ...

  5. activeMq之hello(java)

    消息队列activeMq,   节省响应时间,解决了第三方响应时间长的问题让其他客户可以继续访问, 安装activeMq apache-activemq-5.14.0-bin\apache-activ ...

  6. hdu 4911 求逆序对数+树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=4911 给定一个序列,有k次机会交换相邻两个位置的数,问说最后序列的逆序对数最少为多少. 实际上每交换一次能且只能 ...

  7. imooc movie

    node+mongodb 建站攻略(一期) 用的都是我熟悉的技术,看了别人的开发过程,自己也学到了一些新的知识 生成配置文件 开发结束后,可以使用bower init来生成前端的配置文件. 不过在bo ...

  8. handsontable 排序问题

    排序是表格的基础功能,handsontable也会支持. 有时需求会很复杂,需要自定义排序,或者调用其他排序方法:自定义排序,比较复杂,没做过:今天要用的是调用R中的排序方法. 有两个事件before ...

  9. Legal or Not HDU

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  10. cxgrid显示海量数据

    cxgrid显示海量数据 在默认情况下,cxgrid显示几万条以上的数据会很慢.怎么办? 交下面的属性设为TRUE以后,速度飞快. 但速度是快了,自动计算列的合计值这些功能却失效了,正所谓有得必有失!