__slots__
, you might have noticed
that it comes with a pleasant side-effect: it makes it impossible to assign
extra, garbage attributes to your objects. While normally not a serious
problem, objects with extra, unintentional crud stuck to them can make
object databases hell to work with as you struggle to figure out where it
came from or why it is in your database but not in memory. Therefore, this
semantic feature is handy in addition to reducing the memory footprint of
objects if you have large numbers of them lying around.If you've noticed this, you've also probably noticed that it is damn near impossible to use
__slots__
because you have no control over
what slots are used from your base classes.Here is a solution to this problem that I have been working on for a while: SlotMachine. While it breaks
isinstance
- you weren't using
isinstance
anyway, right? - it does do the sane thing that you
would expect a cooperative __slots__
implementation to do; you
can subclass from random other classes (provided that you properly specify
all their attributes as slots on your object) and other SlotMachines, and
even other objects that define __slots__, without giving up the explicit
specification of attributes or the ability to inherit from things.I'll add some more comments to that file later tomorrow, I think.