class Test(object): __slots__ = ("name","age") t = Test() t.name = "老王" t.age = 18 print(t.name) print(t.age) #__slots__用来控制类可以动态添加的属性 t.addr = "山东"
输出
老王 18 Traceback (most recent call last): File "E:/python_code2019/day1/40-__slots__.py", line 13, in <module> t.addr = "山东" AttributeError: 'Test' object has no attribute 'addr'