__slots_ and inheritance

Paul Rubin http
Thu Apr 10 09:18:09 CEST 2003


Alex Martelli <aleaxit at yahoo.com> writes:
> __slots__ is, essentially, an optimization (in terms of memory
> consumption) intended for classes that are likely to be created 
> in very large numbers: you give up the normal flexibility of
> being able to add arbitrary attributes to an instance, and what
> you gain in return is that you save one dictionary per instance
> (dozens and dozens of bytes per instance -- that will, of course,
> not be all that relevant unless the number of instances that are
> around is at the very least in the tens of thousands).

I generally use __slots__ as a way to "declare" instance variables,
so I get an exception if I accidentally assign to an undeclared one.
I'd hope that was one of __slots__'s intended uses.




More information about the Python-list mailing list