class Point(object): __slots__ = ('name','point') p1 = Point() p1.name = 100 print(p1.name)#100 #p1.score = 200#因为'score'没有被放到__slots__中,因此不能绑定score属性,试图绑定score将获得AttributeError的错误 #使用__slots__要注意,__slots__定义的属性仅对当前类实例起做用,对继承的子类是不起做用的 #除非在子类中也定义__slots__,这样,子类实例容许定义的属性就是自身的__slots__加上父类的__slots__ class PointExtend(Point): pass p2 = PointExtend p2.score = 200 print(p2.score)#200