[Python-3000] ABC PEP isinstance issue Was: PEP 31XX: A Type Hierarchy for Numbers (and other algebraic entities)

Calvin Spealman ironfroggy at gmail.com
Sat Apr 28 20:27:30 CEST 2007

On 4/28/07, Guido van Rossum <guido at python.org> wrote:
> On 4/28/07, Jean-Paul Calderone <exarkun at divmod.com> wrote:
> > Aside from the way in which `x' can already lie:
> >
> >     >>> class X(object):
> >     ...     __class__ = property(lambda self: int)
> >     ...
> >     >>> isinstance(X(), int)
> >     True
> >     >>>
> >
> > Is this behavior changed/going to be changed in Py3k?
> I'm not particularly enamored with it, but I believe it once served a
> purpose for Zope. Does anyone know if it is still needed?
> --
> --Guido van Rossum (home page: http://www.python.org/~guido/)

Consider this example:

>>> class X(object):
>>> class Y(object):
	a = 10
>>> x = X()
>>> x.__class__ = Y
>>> x.a
>>> isinstance(x, Y)

Now, I'm not saying we must keep this behavior, but it does have its
merits. In particular, the cases of type-changing objects, which I'm
not convinced always have a place, but thats not the point. The point
I want to make is that x is not lying about its type. It IS a Y,
because __class__ determines its type. The breakage may not be that x
could lie and say its an int, but that it actually can't its a
deception of the class, not the type, because you can't assign
x.__class__ = int, for example. Either the behavior should work for
any type, thus allowing heap types to be assigned to __class__
attributes, changing the type of objects (but, obviously immutables
would disallow __class__ assignment). Or, an objects type should not
be determined simply by the value of attributes, __class__, at all.

Does that make sense?
Read my blog! I depend on your acceptance of my opinion! I am interesting!

More information about the Python-3000 mailing list