[Python-Dev] operator.is*Type
Ian Bicking
ianb at colorstudy.com
Wed Feb 22 18:10:14 CET 2006
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 they are just identical...? They seem terribly pointless to me.
Deprecation is one option, of course. I think Michael's suggestion also
makes sense. *If* we distinguish between sequences and mapping types
with two functions, *then* those two functions should be distinct. It
seems kind of obvious, doesn't it?
I think hasattr(obj, 'keys') is the simplest distinction of the two
kinds of collections.
> 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.
I think you are reading too much into it. If the functions exist, they
should be useful. That's all I see in Michael's suggestion.
--
Ian Bicking / ianb at colorstudy.com / http://blog.ianbicking.org
More information about the Python-Dev
mailing list