[Python-Dev] Global __slots__ pondering

Michael Hudson mwh at python.net
Wed Mar 10 06:09:01 EST 2004


François Pinard <pinard at iro.umontreal.ca> writes:

> Hi, people.
>
> Since a few days, I'm working at a Python rewrite of a difficult and
> complex C application, written by a "power" programmer years ago, who
> left the company before I came in.  The rewrite goes extremely well,
> Python legibility and facilities are such that I could improve the angle
> of attack for the problem, making it appear much, much easier.
>
> The C code mallocs a lot, and the Python rewrite is memory intensive.
> Since it handles hundreds of thousands of objects, I thought it was
> a good case for using `__slots__' and indeed, the memory savings are
> interesting.  Deriving from built-in types for a good amount of objects
> (for a limited amount of types), whenever appropriate, is also welcome.
>
> In one module, sub-classing is frequent, and if I understand well how
> it works, a `__dict__' is used in sub-types unless `__slots__ = ()'
> is explicitely included in the sub-type, and I decided to spare that
> `__dict__' whenever possible in that particular module.
>
> So, just after the global `__metaclass = type' at the beginning of the
> module, I added a global `__slots__ = ()', with the clear intent of
> overriding that `__slots__' assignment only for those particular types
> needing slots.  In this module of the application, I do not mind having
> to restrain all types so none uses a `__dict__'.
>
> But it does not seem to work that way.  Could it be pondered that, the
> same as `__metaclass__' may have a global value, `__slots__' may also
> have?

You can ponder that, but it's a sick idea, so there :-)

> One objection I see is that, while __metaclass__ may be redefined to
> the classic class metatype for a specific type, a global `__slots__'
> could not be arbitrarily defeated in a specific type.  Nevertheless,
> in the precise case I have, it would have been a bit useful.
>
> I do not mind much having to repeat `__slots__' in each and every class
> and sub-class for this module, 

One thing you can do is have a custom metaclass that sticks
'__slots__':() into the namespace before calling the superclass'
__new__ method.

Cheers,
mwh

-- 
  If I didn't have my part-time performance art income to help pay
  the  bills, I could never afford to support my programming 
  lifestyle.                                -- Jeff Bauer, 21 Apr 2000



More information about the Python-Dev mailing list