__dict__:实例化类时将实例属性分配到__dict__

__slots__:阻止实例分配属性到__dict__

 

class Base(object):
    __slots__ = ['y', 'x']
    def __init__(self):
        self.y = 'aa'
        self.x = 'xx'


b = Base()
print b.__dict__ #抛错
print b.y
b.x = 30
print b.x

  

相关文章:

  • 2022-12-23
  • 2021-05-27
  • 2021-09-02
  • 2021-07-16
  • 2022-12-23
  • 2022-12-23
  • 2021-10-01
猜你喜欢
  • 2021-08-06
  • 2021-08-29
  • 2021-08-24
  • 2021-06-02
  • 2022-12-23
  • 2022-02-14
  • 2022-12-23
相关资源
相似解决方案