[Python-bugs-list] [ python-Bugs-532767 ] isinstance() should respect __class__

noreply@sourceforge.net noreply@sourceforge.net
Thu, 21 Mar 2002 05:57:31 -0800


Bugs item #532767, was opened at 2002-03-20 22:22
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=532767&group_id=5470

Category: Type/class unification
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Steve Alexander (stevea_zope)
Assigned to: Guido van Rossum (gvanrossum)
Summary: isinstance() should respect __class__

Initial Comment:
isinstance(obj, class_or_type_or_tuple) should compare
using obj.__class__ when obj is an instance of a type
or a new-style class.

This is important for using weak references and other
kinds of proxy wrappers, where you want to pass a proxy
to some code, which might query its type using isinstance.

issubclass may need a similar treatment.

----------------------------------------------------------------------

>Comment By: Steve Alexander (stevea_zope)
Date: 2002-03-21 13:57

Message:
Logged In: YES 
user_id=492001

oops... please ignore my unsupported use of "is" to compare
small ints.

I meant:

from Zope.ContextWrapper import Wrapper

wl = Wrapper([])
assert isinstance(wl, list) == 1
assert isinstance(wl, Wrapper) == 1



----------------------------------------------------------------------

Comment By: Steve Alexander (stevea_zope)
Date: 2002-03-21 13:55

Message:
Logged In: YES 
user_id=492001

A new isinstance can maintain and extend the semantic Martin
describes.

Let's say object wl is a wrapped list:

from Zope.ContextWrapper import Wrapper

wl = Wrapper([])
assert isinstance(wl, list) is 1
assert isinstance(wl, Wrapper) is 1

So, your semantics are maintained. With the proposed change,
the property you describe need not be given up.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-03-21 13:47

Message:
Logged In: YES 
user_id=21627

There has been a long-standing guarantee that 'type(o) is X'
implies 'isinstance(o, X)', or, more compact,
'isinstance(o,type(o))' for all objects o. In fact, people
have been advised to change the explicit test for type() to
isinstance calls. With the proposed change, this property
will be given up.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-03-21 13:16

Message:
Logged In: YES 
user_id=6380

I give this a +1. To refute Martin's -1: The use case that
prompts this is passing a wrapper to wrapper-ignorant code.
This may be 3rd party code that you can't afford to make
wrapper-aware. If I pass you a wrapper to an X where you
expect an X, your isinstance(x, X) call should succeed.
Especially since x.__class__ already returns X. Also,
isinstance(x, X) succeeds if X is a classic class and x is a
wrapped X instance.

If you want to know if something is a wrapper, you have to
use type().

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-03-21 08:59

Message:
Logged In: YES 
user_id=21627

-1. That means that you can't use isinstance anymore to
determine whether something is a weak reference, or other
kind of proxy wrapper.

If you need a function that unwraps wrappers, write one
yourself.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=532767&group_id=5470