__slots__用法

 

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 
    t.addr = "山东"
AttributeError: 'Test' object has no attribute 'addr'

  

 

转载于:https://www.cnblogs.com/xone/p/10271427.html

你可能感兴趣的:(__slots__用法)