can't add variables to instances of built-in classes

Ethan Furman ethan at stoneleaf.us
Mon Jul 18 09:10:08 EDT 2016


On 07/17/2016 04:50 AM, Wilson Ong wrote:

>> Use this feature sparingly, only when you know that there are going to be
>> many (millions rather than thousands) of Test instances.
>
> Why use it sparingly? Is it for extensibility? What if I'm pretty sure that
>  my class is going to have exactly these attributes only?

One of the very nice things about Python is being able to fiddle with objects, both
for debugging and for extending.  One of the ways that's accomplished is by adding
attributes to instances, but when __slots__ is defined that ability is lost, which
can make using/debugging those types of objects harder.

__slots__ is a memory optimization, and like most optimazations you shouldn't use
it until you know you need it.

--
~Ethan~



More information about the Python-list mailing list