-
Book Overview & Buying
-
Table Of Contents
Python Essentials
By :
The default behavior of the object superclass is to create a dict for an object's attributes. This provides fast resolution of names. It means that an object can have attributes added and changed very freely. Because a hash is used to locate the attribute by name, the internal dict object can consume quite a bit of memory.
We can modify the behavior of the object superclass by providing a list of specific attribute names when we create a class. When we assign these names to the specially named __slots__ variable, these will be the only available attributes. A dict is not created, and memory use is reduced considerably.
If we're working with very large datasets, we might need to use a class that looks like this:
class SmallSample:
counter= 0
__slots__ = ["sequence", "measure"]
def __init__(self, measure):
SmallSample.counter += 1
self.sequence = SmallSample.counter
self.measure = measureThis class...