代码
class student:
__slots__ = ("name","age") #使用slots语句,限制类只可以有name,age这两个属性
s = student();
s.name = "jeek"
print(s.name)
s.age = 13
print(s.age)
s.score = 100 #这里给类动态加载了新的属性,程序会报<AttributeError>错误
print(s.score)
结果
jeek
13
Traceback (most recent call last):
File "C:\Users\Administrator\documents\visual studio 2017\Projects\PythonApplication1\PythonApplication1\PythonApplication1.py", line 9, in <module>
s.score = 100
AttributeError: 'student' object has no attribute 'score'