class Person(object):
__slots__ = ('name', 'age')
p = Person()
p.name = 'aaa'
p.age = 11
p.sex = 'nn'
print(p.name,p.age,p.sex)
Traceback (most recent call last):
File "/Users/yjx/Desktop/yjx/slots的作用.py", line 20, in <module>
p.sex = 'nn'
AttributeError: 'Person' object has no attribute 'sex'
__slots__定义的属性仅对当前类实例起作用,对继承的子类是不起作用的