""" 类属性和对象属性 当对象的类属性没有修改时,会和类共用类属性。 当对对象的类属性进行修改,则对象会保存该属性,不再和类共用一个类属性。 """ class People(object): money = 1000 def __init__(self, name, age, gender=1): self.name = name self.age = age self.gender = gender print(id(self.money)) print(id(People.money)) student1 = People("zhangsan", 20) student2 = People("lisi", 25) print(student1.name) print(student2.name) output: 2103971257872 2103971257872 2103971257872 2103971257872 zhangsan lisi People.money = 4 print(id(People.money)) print(id(student1.money)) print(id(student2.money)) print(People.money) print(student1.money) print(student2.money) output: 1399634480 1399634480 1399634480 4 4 4 student1.money = 1 student2.money = 2 print(id(People.money)) print(id(student1.money)) print(id(student2.money)) print(People.money) print(student1.money) print(student2.money) output: 1399634480 1399634384 1399634416 4 1 2class animal: count = 0 def __init__(self): animal.count += 1 x = animal() x.count output: 1 y = animal() y.count output: 2