[Python-Dev] Should issubclass() be more like isinstance()?

Alex Martelli aleaxit at gmail.com
Wed Apr 5 05:41:32 CEST 2006


On Apr 4, 2006, at 8:18 PM, Crutcher Dunnavant wrote:
 ...
>> There's no rule that predicate cannot raise an exception.
>> No, but it makes many applications (such as using it as a test in list
> comprehensions) difficult enough to not be worth it.

IMHO, the solution to THAT very real problem is to supply a built-in 
function that catches some exceptions and maps them into suitable 
return values. Simplest would be something like:
def catch(excepts, default, f, *a, **k):
 try:
 return f(*a, **k)
 except excepts:
 return default
and then, the LC you're after is easy:
subints = [x for x in y if catch(TypeError, False, issubclass, x, int)]
though I'm sure we can get better syntax if we turn 'catch' into some 
kind of syntactic special form. My point is that there are umpteen 
predicates one can write which would have to be distorted to ensure 
they can't raise -- better to let them raise if they must, and allow 
catching the expected exception(s), somewhat like this example.
Alex


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /