[Python-Dev] operator.is*Type

Raymond Hettinger python at rcn.com
Wed Feb 22 12:59:59 CET 2006

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

Your example simply highlights the consequences of one of Python's most basic, 
original design choices (using getitem for both sequences and mappings).  That 
choice is now so fundamental to the language that it cannot possibly change. 
Get used to it.

In your example, the results are correct.  The "anything" class can be viewed as 
either a sequence or a mapping.

In this and other posts, you seem to be focusing your design around notions of 
strong typing and mandatory interfaces.  I would suggest that that approach is 
futile unless you control all of the code being run.


More information about the Python-Dev mailing list