[Python-ideas] having a "iterable" built-in

Pascal Chambon chambon.pascal at wanadoo.fr
Wed Apr 29 21:02:34 CEST 2009


Tarek Ziadé a écrit :
> Hello
>
> unless I missed it, I couldn't find a built-in to check if an object
> is iterable,
>
> so I wrote this function :
>
> def iterable(ob):
>     try:
>         iter(ob)
>     except TypeError:
>         return False
>     return True
>
>
> What about having such a built-in in Python ? (with the proper
> implementation if course)
>
> Regards
> Tarek
>
>   
Well, I guess the new abilities of abstract base classes and stuffs, in 
2.6, allow this : http://docs.python.org/library/collections.html

You should call something like isinstance(ovj, Iterable), which is less 
cute than isiterable(obj), but far more flexible ^^

Regards,
Pascal







More information about the Python-ideas mailing list