Is there a consensus on how to check a polymorphic instance?

Alex Martelli aleaxit at yahoo.com
Sun Nov 28 19:46:12 EST 2004


Peter Hansen <peter at engcorp.com> wrote:

>  > But the problem is still there: how do you know what
> > it looks like before you treat it as a cat? isinstance(), , as Steve
> > state, is too rigid.
> 
> The argument is that you probably don't really need to know
> what it looks like *before* you try to use it, even if you
> think you do.  The cases where you really do are probably those
> Alex Martelli's recipe (mentioned near the start of the thread)
> is intended to help.  (Specifically, avoiding the situation
> where the object implements the required protocol only partially,
> but you don't find that out until you've already started using
> the object, possibly corrupting it in the process.)

Exactly.  The recipe's still in the CB 2nd edition (which I should be
editing right now instead of doing usenet, with deadline so terribly
close and lots of enhancements left to do, but hey, it IS 1:30 AM, I
deserve a little break;-).

The right solution is adaptation (PEP 246, Eby's PyProtocols, etc), but
until we can convince Guido of that, so that adaptation becomes
widespread, duck typing, and occasionally (when needed) "accurate LBYL"
remain the best approach.

And don't forget unit-tests -- see Robert Martin's now-famous article at
http://www.artima.com/weblogs/viewpost.jsp?thread=4639 .


Alex



More information about the Python-list mailing list