Slots are a nice way to work around this space consumption problem. Instead of having a dynamic dict that allows adding attributes to objects dynamically, slots provide a static structure which prohibits additions after the creation of an instance.
请记住,这是有代价的
It will break serialization (e.g. pickle). It will also break multiple inheritance. A class can't inherit from more than one class that either defines slots or has an instance layout defined in C code (like list, tuple or int).
您可以使用
__slots__
来实现这一点,您可以使用magic方法__slots__
在这里找到更多信息:Slots docs请记住,这是有代价的
我希望这些信息是足够的
相关问题 更多 >
编程相关推荐