Is 'isinstance()' the right thing?

Erik Max Francis max at alcyone.com
Tue Apr 30 05:08:58 EDT 2002


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()'. However, in general the type of an argument is
> not the crux but whether the argument supports a certain
> 'interface' or 'protocol'. Thus, a function 'hasinterface()'
> would appear more natural (more 'Pythonic') to me.
> 
> 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.

Depending on what you want, some of the is... functions in the operator
module might be to your liking.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Life is one long process of getting tired.
\__/ Samuel Butler
    Interstelen / http://www.interstelen.com/
 A multiplayer, strategic, turn-based Web game on an interstellar scale.



More information about the Python-list mailing list