On 4/28/07, Calvin Spealman <[EMAIL PROTECTED]> wrote:
> On 4/28/07, Guido van Rossum <[EMAIL PROTECTED]> wrote:
> > On 4/28/07, Jean-Paul Calderone <[EMAIL PROTECTED]> 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/)
_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to