[Python-Dev] Global slots pondering (original) (raw)

François Pinard pinard at iro.umontreal.ca
Tue Mar 9 17:19:06 EST 2004


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? 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, yet I thought about sharing the above thoughts with Python developers, in case they would like to level the said difference. It would be less surprising if metaclass' and `slots' could equally be implied globally. Or maybe? :-)

-- François Pinard http://www.iro.umontreal.ca/~pinard



More information about the Python-Dev mailing list