Bugs item #532767, was opened at 2002-03-20 17:22 You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=532767&group_id=5470 Category: Type/class unification Group: Python 2.3 >Status: Closed >Resolution: Accepted Priority: 6 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: Guido van Rossum (gvanrossum) Date: 2003-02-11 22:41 Message: 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. ---------------------------------------------------------------------- Comment By: Guido van Rossum (gvanrossum) Date: 2002-03-21 09:52 Message: 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. ---------------------------------------------------------------------- Comment By: Steve Alexander (stevea_zope) Date: 2002-03-21 08: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 08: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 08: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 08: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 03: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: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=532767&group_id=5470
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4