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 _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com