[Python-Dev] operator.is*Type

Thomas Heller theller at python.net
Wed Feb 22 13:03:55 CET 2006


Fuzzyman wrote:
> Hello all,
> 
> Feel free to shoot this down, but a suggestion.
> 
> The operator module defines two functions :
> 
>     isMappingType
>     isSquenceType
> 
> 
> These return a guesstimation as to whether an object passed in supports 
> the mapping and sequence protocols.
> 
> These protocols are loosely defined. Any object which has a 
> ``__getitem__`` method defined could support either protocol.

The docs contain clear warnings about that.

> I suggest we either deprecate these functions as worthless, *or* we 
> define the protocols slightly more clearly for user defined classes.

I have no problems deprecating them since I've never used one of these
functions.  If I want to know if something is a string I use isinstance(),
for string-like objects I would use

  try: obj + ""
  except TypeError:

and so on.

> 
> An object prima facie supports the mapping protocol if it defines a 
> ``__getitem__`` method, and a ``keys`` method.
> 
> An object prima facie supports the sequence protocol if it defines a 
> ``__getitem__`` method, and *not* a ``keys`` method.
> 
> As a result code which needs to be able to tell the difference can use 
> these functions and can sensibly refer to the definition of the mapping 
> and sequence protocols when documenting what sort of objects an API call 
> can accept.

Thomas



More information about the Python-Dev mailing list