An Object's Type

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Wed Dec 5 15:58:29 EST 2007


Neil Cerutti a écrit :
> 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.

Yeps, this is one of the couple occasions you might want to know 
something about the object's type. The few other use case I had where 
mostly about the same pattern - "low-level" and somewhat dirty code.

> 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).
> 

!-)



More information about the Python-list mailing list