Is there a consensus on how to check a polymorphic instance?
peter at engcorp.com
Wed Nov 24 16:24:21 CET 2004
Mike Meng wrote:
> It seems to me the virtue of dynamic langauge is, if it looks like a
> cat, it's a cat.
I think this is more accurately expressed as "if it *acts* like
a cat, then you can treat it like a cat". Your version implies
that you really do care what it _is_, which goes back to the
static typing thinking again.
> 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.)
More information about the Python-list