将__slots__动态添加到导入的类中

Jzl*_*325 5 python python-2.7

请看以下示例:

from foo import Bar

b = Bar
print(b.__slots__)
#Stack trace, no attribute __slots__
Run Code Online (Sandbox Code Playgroud)

然后假设我们要添加一个__slots__属性,因为Bar将创建很多(和很多)实例.

from foo import Bar

CBar = Bar
CBar.__slots__ = ["one", "two", "three"]

b = CBar
print(b.__slots__)
#Success

b.four = 0
print(b.four)
#Prints 0....hmmm....
Run Code Online (Sandbox Code Playgroud)

是否可以从模块导入类并__slots__动态添加属性?