function taking scalar or list argument
steven.bethard at gmail.com
Tue Aug 24 08:16:48 CEST 2004
Terry Reedy <tjreedy <at> udel.edu> writes:
> In 2.2.1:
> >>> hasattr([1,2,3], '__iter__')
> so this does not seem to work as you seem to expect. hasattr(iter(x),
> '__iter__') will.
Yeah, I thought there was probably some special case I was missing there. The
problem with hasattr(iter(x), '__iter__') is that iter(x) can throw an
exception if the object is not iterable:
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: iteration over non-sequence
So if you call iter(x) in the test, and x is a number, you end up having to
try and catch the exception just like in the original solution.
Do you know at what version hasattr(x, '__iter__') started working? In 2.4a2:
>>> hasattr([1,2,3], '__iter__')
More information about the Python-list