[Python-3000] Thoughts on collections.Container and collections.Iterable

Nick Coghlan ncoghlan at gmail.com
Sat Feb 9 03:23:24 CET 2008


Guido van Rossum wrote:
> I would need to think more about this. I'm tempted not to do this, and
> let these ABCs denote the *explicit* presence of __contains__ and
> __iter__, respectively. Something that's iterable but doesn't
> implement __contains__ supports the 'in' operator very inefficiently
> (through linear search) which we might not want to encourage.

I think a bigger conceptual problem with doing containment tests on 
arbitrary iterables is that you may end up consuming an iterator in the 
process of checking whether it contains the item you want (i.e. if 
Iterable defines __contains__, then Iterator will acquire it by 
inheritance).

So "assert (x in obj) == (x in obj)" may well fail in some cases if we 
adopt the idea that being iterable implies support for containment tests.

Random thought inspired by the __contains__ code in Raymond's post: does 
anyone else think a 'key' parameter on any() and all() would be useful?

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list