[Python-Dev] operator.is*Type (original) (raw)
Bob Ippolito bob at redivi.com
Wed Feb 22 18:04:38 CET 2006
- Previous message: [Python-Dev] operator.is*Type
- Next message: [Python-Dev] operator.is*Type
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: [Python-Dev] operator.is*Type
- Next message: [Python-Dev] operator.is*Type
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]