# 注意:__slots__ 用来限制当前类的实例属性的,如:name、age才可被使用,添加其他的属性则报错 # 不会限制继承类的属性 class Person(): __slots__ = ("name", "age") def __init__(self): pass class Yang(Person): def __init__(self): pass if __name__ == "__main__": p = Person() p.name = "yy" p.age = 18 # p.id = 10 # 'Person' object has no attribute 'id' y = Yang() y.id = 18 pass