类__slots__与__dict__用法

__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

  

posted @ 2013-01-15 15:17  践道者  阅读(210)  评论(0编辑  收藏  举报