Object type check

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Wed Feb 7 15:44:02 EST 2007


king kikapu a écrit :
> Hi to all,
> 
> in statically-types languages, let's say C# for example, we use
> polymorphism through interfaces. So we define an interface I with
> method M and then a class C that implements I interface and write code
> for the M method.
> So, if we have a function that takes a parameter of type I, we know
> before-hand that it will have an M method to call.
> 
> But in dynamic languages this is not the case and we can pass whatever
> we want to that function.

Yes. And this is a Good Thing (tm).

> Assuming that someone writes a library in
> Python that other programmers will use, what is the correct way to
> check inside that function if the parameter passed is of the correct
> type, maybe "isinstance" BIF ?

The correct way in Python is to *not* check.  Just document what kind of
interface you expect, and it's ok. If the people using your code are stupid
and lazy enough to not read the doc, then you can't help .




More information about the Python-list mailing list