__slots_ and inheritance

Tim Peters tim.one at comcast.net
Sun Apr 13 01:30:37 EDT 2003


[Michael Chermside]
> As Alexander Schmolck has so eloquently explained, use of
> __slots__ (which is intended simply as a speed-and-memory optimization)
> breaks several things...  among them:
>  1. It breaks weakrefs (can't make a weakref to a __slots__ object).
>  2. It breaks pickling (can't pickle a __slots__ object without
>     custom code).
>  3. It breaks introspection (eg: can't call vars() on a __slots__ object).
>  4. It breaks "decoration" with new attributes (can't add new attributes
>       if object has no __dict__).
>
> Of these, #4 was intentional, but I don't see any reason for the others!
> For instance, introspection (most of it anyhow) could be enabled for
> __slots__ objects by modifying vars() (and several other things) to
> check the __slots__ for a list of additional values. Pickling too could
> set and get values listed in __slots__. I haven't any idea what the
> issue with weakrefs would be.
>
> So can anyone out there tell me if these are (a) planned, but not done
> yet, or (b) welcome, patches would be appreciated, or (c) difficult or
> impossible for some reason I'm not aware of.

A mix of all, but mostly #b and #c now.  #2 is repaired in Python 2.3, but
required a new pickle protocol, and lots of work:

    http://www.python.org/peps/pep-0307.html







More information about the Python-list mailing list