[Patches] [ python-Patches-1708353 ] Make isinstance/issubclass overloadable for PEP 3119

SourceForge.net noreply at sourceforge.net
Sat May 12 21:27:25 CEST 2007


Patches item #1708353, was opened at 2007-04-26 20:16
Message generated for change (Comment added) made by gbrandl
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1708353&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Core (C code)
Group: Python 3000
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Guido van Rossum (gvanrossum)
Assigned to: Guido van Rossum (gvanrossum)
Summary: Make isinstance/issubclass overloadable for PEP 3119

Initial Comment:
I came up with a fairly simple way to overload isinstance() and issubclass().  The class that is the second argument can define a (class) method named __instancecheck__ or __subclasscheck__ which, if present, will be called *instead* of the normal approach.

The names are different to remind users that the calling convention is the opposite -- isinstance(x, C) maps to C.__instancecheck__(x).


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

>Comment By: Georg Brandl (gbrandl)
Date: 2007-05-12 19:27

Message:
Logged In: YES 
user_id=849994
Originator: NO

Probably a warning would be good for the non-classmethod case.

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2007-05-11 21:40

Message:
Logged In: YES 
user_id=6380
Originator: YES

FWIW, this can run into unchecked infinite recursion easily:

class C:
    # There's no @classmethod decorator here as there should have been
    def __instancecheck__(self, arg):
        return False

isinstance(42, C)

The reason is that on line 372 in classobject.c there's a call to
PyObject_IsInstance(self, klass).

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2007-05-11 14:16

Message:
Logged In: YES 
user_id=6380
Originator: YES

Version 3 compiles with older C89 compilers like gcc 2.96.
File Added: typechecks.diff

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2007-04-26 21:27

Message:
Logged In: YES 
user_id=6380
Originator: YES

I'll add a description and motivation for this to PEP 3119.

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2007-04-26 20:34

Message:
Logged In: YES 
user_id=6380
Originator: YES

Oops, forget the first attempt; isinstance(Integer(), Integer) would be
False!  Now it's True, and more tests are added.
File Added: typechecks.diff

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

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1708353&group_id=5470


More information about the Patches mailing list