Is 'isinstance()' the right thing?
Bill Dozier
billdozier at hotmail.com
Tue Apr 30 09:34:13 EDT 2002
Erik Max Francis <max at alcyone.com> wrote in message news:<3CCE5F2A.1103E744 at alcyone.com>...
> Ralf Juengling wrote:
>
> > when I want to make sure within a function that it can deal with
> > the arguments passed, the only choice is to check it's type via
> > 'isinstance()'.
> > So, what is the proper, 'pythonic' way of argument checking?
>
> The "Pythonic" way of argument checking is to simply use it and catch
> the exceptions that will be thrown if it doesn't behave as planned.
>
IMHO, "isinstance()" is usually not the right thing. Whether it's
"pythonic" or not, I can't say.
When you have code that checks the type of an object and then treats
it accordingly, it's usually a sign that you need to use some
polymorphism. Of course, you don't always have the luxury of fixing
the design.
Bill
More information about the Python-list
mailing list