[Python-Dev] A small step to removing the type/class split

Guido van Rossum guido@python.org
Tue, 24 Oct 2000 16:50:32 -0500


> I've run into a problem with ExtensionClass which I believe can
> only be fixed by modifying Python.  I will try to explain.
> 
> I have an ExtensionClass which defines __cmp__.  Depending on the
> objects being compared, the __cmp__ method may never be called.
> This is due to code in object.c that looks like this:
> 
>     if (PyInstance_Check(v) || PyInstance_Check(w)) {
>         try to use use __cmp__ method
>     }
>     else {
>         try number coerce and fall back on type name comparison
>     }
> 
> Extension classes can never pass the PyInstance_Check predicate.
> I've searched for all occurances of PyInstance_Check in the 2.0
> source.  In many places that PyInstance_Check occurs a more
> general "is this an instance-like type" check would seem to be
> more suitable.
> 
> Here is my proposal:
> 
>   * Define a new type flag Py_TPFLAGS_INSTANCE.
>   * Create a new predicate Py_IsInstance which checks for this
>     flag.
>   * Set this flag on PyInstance_Type.
>   * Replace most occurances of PyInstance_Check with
>     Py_IsInstance.
> 
> Extension types (like ExtensionClass) can then define the type
> flag Py_TPLAGS_INSTANCE and be treated as an instance type by the
> Python interpreter.  This should make it quite a bit easier to make
> extension types behave like "real" classes.
> 
> Comments?

Good analysis of a problem -- but I disagree on the solution.

Rather than formalizing the exceptions made for instances, we should
work on eradicating the differences between classes and types.  I
believe that the plans for rich comparisons would or could also take
care of this.  I hope I'll have more time soon to explore this.

--Guido van Rossum (home page: http://www.python.org/~guido/)