#定义一个类Student class Student(object): __slots__ = ('name','age') #用元组(tuple)的形式绑定属性名称 s = Student() s.name = 'xh' print s.name #xh #s.score = 88 #因为 __slots__中没有score属性,所以报错 #print s.score #AttributeError: 'Student' object has no attribute 'score' #定义一个类MidStudent继承于Student class MidStudent(Student): pass m = MidStudent() m.name = 'xm' print m.name #xm #虽然父类对属性进行了限制,但子类不受影响 m.score = 88 print m.score #88