[Python-Dev] operator.is*Type

Fuzzyman fuzzyman at voidspace.org.uk
Wed Feb 22 12:33:03 CET 2006


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.

Therefore :

 >>> from operator import isSequenceType, isMappingType
 >>> class anything(object):
...     def __getitem__(self, index):
...         pass
...
 >>> something = anything()
 >>> isMappingType(something)
True
 >>> isSequenceType(something)
True

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

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.

All the best,

Michael Foord


More information about the Python-Dev mailing list