[Python-Dev] How to suppress instance __dict__?
Guido van Rossum
Sun, 23 Mar 2003 08:21:12 -0500
> I am generating extension types derived from a type which is derived
> from int 'int' by calling the metaclass; in order to prevent instances
> of the most-derived type from getting an instance __dict__ I am
> putting an empty tuple in the class __dict__ as '__slots__'. The
> problem with this hack is that it disables pickling of these babies:
> "a class that defines __slots__ without defining __getstate__
> cannot be pickled"
> Yes, I can define __getstate__, __setstate__, and __getinitargs__ (the
> only one that can actually do any work, since ints are immutable),
> but I was wondering if there's a more straightforward way to suppress
> the instance __dict__ in the derived classes.
Actually, even __getinitargs__ won't work, because __init__ is called
after the object is created. In Python 2.3, you'd use __getnewargs__,
but I expect you're still bound to supporting Python 2.2 (Python 2.3
also doesn't have the error message above when pickling).
I think you could subclass the metaclass, override __new__, and delete
the bogus __getstate__ from the type's __dict__. Then you'll get the
default pickling behavior which ignores slots; that should work just
fine in your case. :-)
--Guido van Rossum (home page: http://www.python.org/~guido/)