Object type check

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


king kikapu a écrit :
>>Dont restrict them to particular types. You would
>>not restrict them to a particular class in C#. Instead, you define the
>>interfaces simply by how you use the objects.
> 
> 
> Of cource i restrict them to particular types! In C# you cannot pass
> something bad
> this way because the compiler will just catch it!

This won't prevent some stupid to implement the interface with a totally 
different semantic (what about some getter wiping out your hard drive ?).

FWIW, in Python, you can't pass something bad because it will raise an 
exception. Well, to be true, there's *one* gotcha : passing a string 
where a list or tuple is expected may in some occasions yield strange 
results (strings being sequences too).

> I see what you mean by "duck typing". So you suggest the "do nothing
> at all" direction,
> better document my code so other can see what is expected, right ?

Exactly.



More information about the Python-list mailing list