This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: isinstance() should respect __class__
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: gvanrossum, loewis, stevea_zope
Priority: high Keywords:

Created on 2002-03-20 22:22 by stevea_zope, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Messages (8)
msg9841 - (view) Author: Steve Alexander (stevea_zope) Date: 2002-03-20 22:22
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.
msg9842 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-03-21 08:59
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.
msg9843 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-03-21 13:16
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().
msg9844 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-03-21 13:47
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.
msg9845 - (view) Author: Steve Alexander (stevea_zope) Date: 2002-03-21 13:55
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.
msg9846 - (view) Author: Steve Alexander (stevea_zope) Date: 2002-03-21 13:57
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

msg9847 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-03-21 14:52
Logged In: YES 
user_id=6380

I'm not sure I like SteveA's suggestion of making
isinstance() respond both to the wrapper type and to the
wrapped type. Although there's a precedent:

>>> class C: pass # classic class
... 
>>> c = C()
>>> isinstance(c, C)
1
>>> import types
>>> isinstance(c, types.InstanceType)
1
>>> 

I'm also not sure I agree with Martin's assertion that
type(o) is X and isinstance(o, X) should always be
equivalent. Especially with new-style user-defined classes,
we could also require that o.__class__ == X and
isinstance(o, X) should be equivalent (modulo subclassing),
and that would require isinstance() to prefer __class__.

I guess we'll have to look at actual use cases of
isinstance() and wrappers (both weakrefs and Zope3's
transparent wrappers). My expectation is that, since
wrappers try hard to pretend to be the wrapped object,
extending this to the isinstance() test is more useful than
the stricter interpretation.
msg9848 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-02-12 03:41
Logged In: YES 
user_id=6380

OK, checked in. When X is a new-style class, isinstance(x,
X) is now defined as "issubclass(type(x), X) or
issubclass(x.__class__, X)". That seems a very reasonable
definition to me.
History
Date User Action Args
2022-04-10 16:05:07adminsetgithub: 36297
2002-03-20 22:22:56stevea_zopecreate