python中 定义一个类 实例对象可以自定义属性
__slots__ 可以限制
__slots__ 用来限制 实例对象的属性
class Students(object): __slots__ = ['name','age'] 限制实例对象的属性 counts = 1 类属性 def __init__(self): Students.counts += 1 没添加一个实例对象 counts加1 self.age = 1 a = Students() a.name = 'saber' # name属性在 __slots__ 里 所以可以定义属性name并复制 print a.age,a.name ---> 1 saber a.height = 12 # height属性不在 __slots__ 里 所以会报错 ---> AttributeError: 'Students' object has no attribute 'height' print a.height