A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2006-February/061713.html below:

[Python-Dev] operator.is*Type

[Python-Dev] operator.is*TypeBob 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).

-bob

More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4