好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python基础-类变量和实例变量

Python基础-类变量和实例变量 写在前面

大纲:

1. 类变量和实例变量

在Python Tutorial中对于类变量和实例变量是这样描述的:

Generally speaking, instance variables are for data unique to each instance and class variables are for attributes and methods shared by all instances of the class:

通常来说,实例变量是对于每个实例都独有的数据,而类变量是该类所有实例共享的属性和方法。

其实我更愿意用类属性和实例属性来称呼它们,但是 变量 这个词已经成为程序语言的习惯称谓。一个正常的示例是:

class Dog:

    kind = 'canine'         # class variable shared by all instancesdef __init__(self, name):self.name = name    # instance variable unique to each instance 

查看更多关于Python基础-类变量和实例变量的详细内容...

  阅读:58次