[Python-3000] ABC PEP isinstance issue Was: PEP 31XX: A Type Hierarchy for Numbers (and other algebraic entities)
Guido van Rossum
guido at python.org
Sat Apr 28 20:57:37 CEST 2007
On 4/28/07, Calvin Spealman <ironfroggy at gmail.com> wrote:
> 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):
> pass
> >>> class Y(object):
> a = 10
> >>> x = X()
> >>> x.__class__ = Y
> >>> x.a
> 10
> >>> isinstance(x, Y)
> True
>
> 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?
Alas not much. Your code example is about *actually* changing the type
of an object, which is a completely different issue, and is
constrained by the object lay-out (try changing the class where both
classes use different __slots__).
Overriding isinstance is about what classes *claim* that the object is
an instance of them, not for the purpose of changing the object's
behavior or representation, but purely for the sake of API
introspection.
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000
mailing list