On Mon, Oct 1, 2012 at 8:16 AM, Albert-Jan Roskam <[email protected]> wrote: > >I had not considered __slots__ at all. I have read about it; IIRC they >can be uised to "slim down" a class so it uses less memory. Is it true >that this is not used very often?
In a class defined with __slots__, it's up to you whether there should be a slot for __dict__ and __weakref__ (see the weakref module). Usually these aren't included because the point is, as you say, to have a smaller footprint if you need to create thousands of objects. But __dict__ can still be a property that returns a new dict when requested. For example, have you ever used collections.namedtuple? It's basically a tuple with the indexed items mapped to field-name attributes via properties. It's meant for light-weight, heterogeneous data records. namedtuple uses an an empty __slots__ (the only option for tuple subclasses) in order to exclude creation of an instance dict. Instead the __dict__ attribute is a property (_asdict is the getter) that returns a new collections.OrderedDict. _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
