[Python-Dev] Type/class differences (Re: Sets: elt in dict, lst.include)

Neil Schemenauer nas@arctrix.com
Mon, 5 Feb 2001 07:02:22 -0800


On Sun, Feb 04, 2001 at 11:47:26PM -0500, Guido van Rossum wrote:
> Yes, I've often thought that we should be able to heal the split for
> 95% by using a few well-aimed tricks like this.  Later...

I was playing around this weekend with the class/type problem.
Without too much effort I had an interpreter that could to things
like this:

    >>> class MyInt(type(1)):
    ...     pass
    ...
    >>> i = MyInt(10)
    >>> i
    10
    >>> i + 1
    11

The major changes were allowing PyClassObject to subclass types
(ie. changing PyClass_Check(op) to (PyClass_Check(op) ||
PyType_Check(op))), writing a _PyType_Lookup function, and making
class_lookup use it.

The experiment has convinced me that we can allow subclasses of
types quite easily without major changes.  It has also given me
some ideas on "the right way" to solve this problem.  The rough
scheme I can up yesterday goes like this:

    PyObject {
        int ob_refcnt;
        PyClass ob_class;
    }

    PyClass {
        PyObject_HEAD
        char *cl_name;
        getattrfunc cl_getattr;
        PyMethodTable *cl_methods;
    }
        
    PyMethodTable {
        binaryfunc nb_add;
        binaryfunc nb_sub;
        ...
    }

When calling a method on a object the interpreter would first
check for a direct method and if that does not exist then call
cl_getattr.  Obviously there are still a few details to be worked
out. :-)

  Neil