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

Kevin Jacobs jacobs@penguin.theopalgroup.com
Thu, 6 Feb 2003 14:39:45 -0500 (EST)


On 6 Feb 2003, Thomas Heller wrote:

Guido van Rossum <guido@python.org> writes: > 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.

I'm happy with this approach, so long as the tuples can also be keys in a dictionary. ;)

Here is an example of what my metaclass does:

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.

My metaclass does something similar, though it does not affect the storage, only the validity constraints:

def enforce_int(x): if not isinstance(x, (int,long)): raise TypeError

def enforce_str(x): if not isinstance(x, basestring): raise TypeError

class Foo(object): metaclass = ConstraintObject slots = { 'a' : enforce_int, 'b' : enforce_str, 'c' : str }

foo = Foo() foo.a = 1 foo.a = 1.0

TypeError foo.b = '1' foo.b = 1 TypeError foo.c = 1 type(foo.c) == '1'

-Kevin

--

Kevin Jacobs The OPAL Group - Enterprise Systems Architect Voice: (216) 986-0710 x 19 E-mail: jacobs@theopalgroup.com Fax: (216) 986-0714 WWW: http://www.theopalgroup.com