operator module isSequenceType with builtin set produces False

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Dec 19 12:13:41 EST 2007


En Wed, 19 Dec 2007 06:28:03 -0300, MarkE <mark.english at rbccm.com>  
escribi�:

>> Is there a short Pythonic way to determine whether an object is
>> iterable (iteratable ??) that I haven't thought of (getattr(obj,
>> '__iter__') ?). Would operator.isIterable() be at all a useful
>> addition ?

Yes, I think the only way is to try iter(obj) and see if it succeeds  
(that's basically the same as what you do with getattr, but actually  
creates the iterator and checks that it's of the right type).

> And here I probably meant container (although the url says sequence
> when the article meant container, bit like me):
> http://docs.python.org/ref/sequence-types.html
> "Containers usually are sequences (such as lists or tuples) or
> mappings (like dictionaries), but can represent other containers as
> well"
>
> So same question, but perhaps "isContainer()" rather than
> "isIterable()"

"container" is too generic. Perhaps you can look if it has a __len__  
attribute. But anyone could implement a linked list (the common interfase  
don't fit well with those sequence/mapping methods) and would be hard to  
deduce whether it is a container or not without further knowledge about it.

-- 
Gabriel Genellina




More information about the Python-list mailing list