"Nick Vatamaniuc" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Tim,
>
> An object is iterable if it implements the iterator protocol

There are presently two iterator protocols.  The old one will be likely be 
dropped in 3.0 (currently being discussed).

>. A good
> enough check to see if it does is to check for the presense of the
> __iter__() method. The way to do it is:
> hasattr(object,'__iter__')

Sorry, this check for the newer and nicer protocol but not the older one.

>>> hasattr('abc', '__iter__')
False

This may change in 2.6.  The defacto *version-independent* way to determine 
iterability is to call iter(ob).  If it returns an iterator, you can 
iterate; if it raises TypeError, you cannot.  Any it should be patched as 
necessary by the developers to continue doing the right thing in future 
versions.

Terry Jan Reedy



-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to