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

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