Why is there no instancemethod builtin?

George Sakkis gsakkis at rutgers.edu
Sun Jun 19 13:13:41 EDT 2005


"Michele Simionato" wrote:

> In this case I have used hasattr(obj, "__iter__") instead of
> isinstance(obj, list)
> (strings are iterable but do not have __iter__ method). I think hasattr
> is much better
> since it works for tuples and custom iterables too.

The fact that strings don't have __iter__ is an implementation detail
(I can't think of any reason other than historic and perhaps backwards
compatibility for this; iterables should IMHO by definition be exactly
the objects with __iter__). Also, what if tuples and any other
iterables except for lists are considered atomic ? An implementation of
s-expressions would use a single data structure for the nesting (e.g. a
builtin list or a custom linked list) and objects of all other types
should be considered atoms, even if they happen to be iterable.

George




More information about the Python-list mailing list