Erich <[EMAIL PROTECTED]> writes:

> def iterable(item, count_str=False):
>     if not count_str and isinstance(item, str):
>         return False
>     try:
>         iter(item)
>     except:
>         return False
>     return True

> This is just simple boolean test for whether or not an object is
> iterable. I would like to see this in builtins, to mirror callable.

Note that 'callable' is being removed in Python 3.0
<URL:http://www.python.org/dev/peps/pep-3100/#built-in-namespace>. To
mirror this, I recommend you remove usage of your 'iterable' function
too :-)

Seriously, the Pythonic way to handle this is to examine *why* you
need to know whether an object is iterable. The only reason I can
think of that makes any sense is that you're going to actually iterate
over it at some point.

In which case, you should omit this 'iterable' check and just iterate
over the object when you need to. Python will raise the appropriate
exception when it doesn't behave as you expect. No need to LBYL, when
Python will tell you about the problem at the time you need to know.

Or, you could use the equivalent of the suggestion in PEP 3100: just
use 'hasattr(obj, "__iter__")'. But this is inferior to just following
EAFP.

-- 
 \          "I hope if dogs ever take over the world, and they chose a |
  `\    king, they don't just go by size, because I bet there are some |
_o__)                Chihuahuas with some good ideas."  -- Jack Handey |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to