function taking scalar or list argument

Steven Bethard steven.bethard at
Tue Aug 24 02:16:48 EDT 2004

Terry Reedy <tjreedy <at>> writes:
> In 2.2.1:
> >>> hasattr([1,2,3], '__iter__')
> 0
> 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:

>>> iter(1)
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 mailing list