TypeCheck vs IsInstance in C API

Terry Reedy tjreedy at udel.edu
Tue Aug 25 18:56:49 CEST 2015

On 8/25/2015 10:32 AM, random832 at fastmail.us wrote:
> On Mon, Aug 24, 2015, at 22:12, Andrew Wang wrote:
[question snipped]

> And, now, your moment of zen:

Huh??  I fail to see the point of this buggy code.

>> def isa(ob, cl):
>>    try:
>>        raise ob
>>    except cl:
>>        return 1
>>    else: return 0

The extra marginal '>' quote makes it look like this buggy code was 
posted by Andrew, but it comes from random832.  In 3.x, both ob and cl 
must be exceptions and isa(1, int) cannot work. In 2.7, ob must be an 
exception or old-style class (not documented that I see).

 >>> raise 1

Traceback (most recent call last):
   File "<pyshell#32>", line 1, in <module>
     raise 1
TypeError: exceptions must be old-style classes or derived from 
BaseException, not int

Hence, isa(1, int) raises TypeError instead of returning 1. If 'else' is 
changed to 'except', it returns 0 instead of 1.

Terry Jan Reedy

More information about the Python-list mailing list