Behavior of class variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | >>> class a: ... b=5 ... def __init__(self,x,y): ... self.x=x ... self.y=y ... >>> p=a(5,6) >>> q=a(5,6) >>> a.b 5 >>> a.b+=1 >>> p.b 6 >>> q.b 6 >>> q.b-=1 >>> q.b 5 >>> p.b 6 >>> a.b 6 |
如您所见,通过实例的方法更改类变量时,这一点不会反映在其他实例的类变量和类变量中。为什么会这样?
因为
1 2 3 4 5 | q.__dict__ {'b': 4, 'x': 5, 'y': 6} p.__dict__ {'x': 5, 'y': 6} |
这一点在语言参考的"任务说明"一节中有明确说明:
Note: If the object is a class instance and the attribute reference occurs on both sides of the assignment operator, the RHS expression,
a.x can access either an instance attribute or (if no instance attribute exists) a class attribute. The LHS targeta.x is always set as an instance attribute, creating it if necessary. Thus, the two occurrences ofa.x do not necessarily refer to the same attribute: if the RHS expression refers to a class attribute, the LHS creates a new instance attribute as the target of the assignment:
1
2
3
4 class Cls:
x = 3 # class variable
inst = Cls()
inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x as 3This description does not necessarily apply to descriptor attributes, such as properties created with
property() .