[Python-Dev] Python 2.3a1's mandatory use of cyclic GC causes existing applications to fail

Thomas Heller theller@python.net
06 Feb 2003 20:25:52 +0100


Guido van Rossum <guido@python.org> writes:

> > As a last remark (although this is too long already),
> > I'd also like to extend the __slots__ syntax by the
> > ability to express "embedded types", like the types
> > supported by array. This again saves a lot of memory
> > when you know your attribute is always some simple type,
> > which need not become an object.
> 
> Can you suggest a concrete syntax to do this?  Maybe setting __slots__
> to a dictionary mapping names to type specifiers would do it -- and it
> would even be backwards compatible: currently, if __slots__ is a dict,
> its keys are used as slot names and its values are ignored, by virtue
> of the way the type constructor iterates over __slots__.

May I suggest to use an ordered sequence (of pairs) instead of a dict
for __slots__.  Then you also can completely control the layout that
C code sees and construct C compatible structures or other data types
from pure Python code.

<plug>
Here's how ctypes does it: It's named _fields_ instead of __slots__,
and typically you write something like this

  class Point(Structure):
      _fields_ = [("x", "i"),
                  ("y", "i")]

which defines a Python objects having x and y instance vars, which
must be integers.

ctypes extends this so that instead of the (struct module compatible)
format specified "i", also Python objects are allowed which describe
a C data type.

Thomas