[Tutor] generic repr method?
eryksun
eryksun at gmail.com
Mon Oct 1 16:12:36 CEST 2012
On Mon, Oct 1, 2012 at 8:16 AM, Albert-Jan Roskam <fomcl at yahoo.com> 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.
More information about the Tutor
mailing list