class Dog:
kind = 'canine' # class variable shared by all instances,类变量被所有的实例共享
def __init__(self, name):
self.name = name # instance variable unique to each instance,对每个实例对象来说实例变量是独一无二的
>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.kind # shared by all dogs
'canine'
>>> e.kind # shared by all dogs
'canine'
>>> d.name # unique to d
'Fido'
>>> e.name # unique to e
'Buddy'
2.Correct design of the class should use an instance variable instead(类的正确设计应该使用实例变量)
class Dog:
def __init__(self, name):
self.name = name
self.tricks = [] # creates a new empty list for each dog
def add_trick(self, trick):
self.tricks.append(trick)