EquipDev
EquipDev

Reputation: 5941

Should Python 3.4 programs use __slots__ in classes?

The __slots__ attribute for classes was made with Python 2 or earlier, and according to comment for answer to Python __slots__ it appears that Python 3.3 has improved so the advantage on memory size may not be the reason for using __slots__ in for example Python 3.4 programs.

So should I clean up my code and remove the use of __slots__ in classes, or is there still some good reason for using __slots__ even in Python 3.4 programs?

Upvotes: 3

Views: 191

Answers (1)

Ethan Furman
Ethan Furman

Reputation: 69240

By using __slots__ you are telling Python not to have a __dict__ attribute on your instances. This is to save memory in cases where you may have thousands upon thousands of instances. This is the reason to use them in Python 2, and it is the reason to use them in Python 3.

Upvotes: 2

Related Questions