disgrating a list

Neil Cerutti horpner at yahoo.com
Fri Sep 1 21:50:44 CEST 2006

On 2006-09-01, Tal Einat <tal.no.no.spam at gmail.com> wrote:
> Tim Chase wrote:
>> I'm not sure if '__iter__' is the right thing to be looking
>> for, but it seems to work at least for lists, sets,
>> dictionarys (via their keys), etc.  I would use it because at
>> least then you know you can iterate over it
> AFAIK and as seen throughout posts on c.l.py, the best way to
> check if something is iterable is:
> try:
>     iter(obj)
> except TypeError:
>     <obj is not iterable>
> else:
>     <obj is iterable>

That confounds me.  Of course, I'm coming from a C++, where you
never want to throw an exception in a common case, hence the name
'exception'. The Python FAQ does say that raising and catching an
exception is an expensive operation. I see that type-checking is
good to avoid, but type-checking must be better than "abusing"
exceptions in this way. Is the above really a popular idiom?

If so, I guess I'll get used to it.

Neil Cerutti

More information about the Python-list mailing list