sizeof PyTypeObject difference

Tom Widrick lordmacro at
Mon Sep 30 06:01:57 EDT 2002

Interestingly enough, it turned out that the easiest way to look at it for
me was to remember that:

class Accumulator:
      __metaclass__ = type
      __value = 0
      value = property(lambda s: s.__value)
      def add(self, v):
        self.value = self.__value + v
      def sub(self, v):
        self.value = self.__value - v

was the same as

tp_dict = {}
def add(self, v):
    self.__value = self.__value + v
tp_dict['add'] = add
def sub(self, v):
     self.__value = self.__value - v
tp_dict['sub'] = sub
tp_dict['__value'] = 0
tp_dict['value'] = property(lambda s: s.__value)
Accumulator = type('Accumulator', (), tp_dict)

I was able to take a "regular" extension type I was working on, defined a
tp_init type member that implemented the (name, bases, dict) interface and
bam, a metatype. So, I guess it could be argued that metatypes in C are
supported by the class/type unification. It's just easier to use the builtin
functions as helpers when implementing the metatype, (like
PyObject_GenericGetAttr). Nearly all of the functionality of the Type object
could be grafted over into my extension type, so I assume that was the
intended way, and not to subtype PyType_Type.


More information about the Python-list mailing list