How can I verify that a passed argument is an interiblecollection?

Terry Reedy tjreedy at udel.edu
Thu Apr 21 22:35:56 EDT 2005


> On Thu, 21 Apr 2005 09:59:54 -0500, Larry Bates
> <lbates at syscononline.com> wrote:
>
>>2) Or if you not you could see if the argument has next and
>>__iter__ methods (more general solution)
>>
>>if  hasattr(arg, 'next') and not hasattr(arg, '__iter__'):
>>    # perform work on iterable

The 'not' is a mistake and should be deleted.  Iterators *do* have __iter__ 
methods, as suggested by the previous sentence.  But anyway, I believe 
strings (or some builtin) still use __getitem__ instead of __next__, so 
this test is not adequate for everything.

Terry J. Reedy






More information about the Python-list mailing list