[Python-Dev] operator.is*Type

Bob Ippolito bob at redivi.com
Wed Feb 22 18:04:38 CET 2006

On Feb 22, 2006, at 4:18 AM, Fuzzyman wrote:

> Raymond Hettinger wrote:
>>>>>> 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.
>> They are not worthless.  They do a damned good job of differentiating
>> anything that CAN be differentiated.
> But as far as I can tell (and I may be wrong), they only work if the
> object is a subclass of a built in type, otherwise they're broken. So
> you'd have to do a type check as well, unless you document that an API
> call *only* works with a builtin type or subclass.

If you really cared, you could check hasattr(something, 'get') and  
hasattr(something, '__getitem__'), which is a pretty good indicator  
that it's a mapping and not a sequence (in a dict-like sense, anyway).


More information about the Python-Dev mailing list