An Object's Type

Neil Cerutti horpner at yahoo.com
Wed Dec 5 11:44:45 EST 2007


On 2007-12-05, Bruno Desthuilliers
<bruno.42.desthuilliers at wtf.websiteburo.oops.com> wrote:
> bg_ie at yahoo.com a écrit :
>> Is it possible to find out if an object is of a certain type
>> or of a type derived from this type?
>
> You have the answer, thanks to Diez and Christian. Now unless
> you have a *very* compelling reason to check the type of an
> object, *just forget about it*. 9 times out of 10, this is
> fighting against the language's type system (hint: google for
> "duck typing").

As I understand Python practice (and this is pretty limited, so
corrections or other use cases are welcome), type checking is
useful for overloading functions by argument type, e.g.,
__getitem__, which accepts both integers and slice objects as
keys for sequence types. Although in the case of __getitem__, et
al, it seems like the key type could have been unified to slices
(except I suppose the return type also change, er... except for
strings--oh never mind).

-- 
Neil Cerutti



More information about the Python-list mailing list