[Python-ideas] `issubclass` shouldn't be raising exceptions for non-type inputs

Bill Janssen janssen at parc.com
Tue Nov 30 03:22:04 CET 2010


Nick Coghlan <ncoghlan at gmail.com> wrote:

> As far as the original post's request goes, no. issubclass expects to
> be given two types. If you have an insanely polymorphic variable that
> can contain a wide variety of objects, only some of which are type
> instances, then either use isinstance to check first as you are
> already doing, or a try-except block to convert the TypeError to a
> False result (and bundle the combined test into a helper function if
> you're doing it a lot).

Right.  Use isinstance instead; that's what it's there for.

But handling exceptions properly really requires thinking about which
might occur, and what to do if they do occur.  If you don't do that
preemptive thinking about exceptionality, you might as well not bother
catching them at all.

Bill



More information about the Python-ideas mailing list