[Tutor] subtyping builtin type

Zachary Ware zachary.ware+pytut at gmail.com
Tue Dec 31 17:39:13 CET 2013


On Tue, Dec 31, 2013 at 10:21 AM, Mark Lawrence <breamoreboy at yahoo.co.uk> wrote:
> The glossary entry for __slots__ states "A declaration inside a class that
> saves memory by pre-declaring space for instance attributes and eliminating
> instance dictionaries. Though popular, the technique is somewhat tricky to
> get right and is best reserved for rare cases where there are large numbers
> of instances in a memory-critical application."  I'll admit that I really
> don't understand what's tricky about it, can someone explain please.

I'm not sure, exactly.  My suspicion is that it can be rather annoying
to make sure you don't add just one extra attribute that isn't in
__slots__ (yet), or to make sure that you found every attribute you
ever access or might need to access in every branch of every method of
everything that uses the class or an instance thereof (since using
__slots__ means you can't add anything to the class dynamically,
unless you add __dict__ to __slots__ which defeats half the purpose).
Also, it's probably meant to add just a faint touch of FUD since it
really isn't necessary 95+% of the time, especially for newbies.

-- 
Zach


More information about the Tutor mailing list