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__定義的屬性僅對(duì)當(dāng)前類實(shí)例起作用唬党,對(duì)繼承的子類是不起作用的