[Python-3000] Could isinstance/issubclass overriding be dangerous? (original) (raw)

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Apr 30 08:27:23 CEST 2007


Guido van Rossum wrote:

Unless they were looking at classic classes, why wouldn't they be using the specific PyCheck() macros?

I'm thinking of Pyrex code. One of the goals of Pyrex is that you should be able to write it without needing to know about the Python/C API. One of the places that's not possible at the moment is in binary operator methods, where you need to write things like

cdef class C:

 def __add(x, y):
   if PyObject_TypeCheck(x, C):
     # we're the left operand
   elif PyObject_TypeCheck(y, C):
     # we're the right operand

I'd like to be able to provide optimised access to isinstance() so that you can think in Python instead of C and write

 def __add(x, y):
   if isinstance(x, C):
     # we're the left operand
   elif isinstance(y, C):
     # we're the right operand

But it seems like isinstance() is already the wrong thing to use for this, and there is currently no Python-level function that does what is needed here.

So can we please have another couple of functions that just do a simple, reliable concrete type test?

-- Greg



More information about the Python-3000 mailing list