[Python-ideas] Make len() usable on a generator

Scott Dial scott+python-ideas at scottdial.com
Sat Oct 4 02:33:30 CEST 2014


On 2014-10-03 11:32 AM, Nick Coghlan wrote:
> list(itr) and sum(itr) are both O(n) operations - the time they take
> is proportional to the length of the input.
> len(container) by contrast, is an O(1) operation - the time it takes
> is independent of the length of the input.

What about collections.ChainMap (computes a union)?

https://hg.python.org/cpython/file/3.4/Lib/collections/__init__.py#l795

What about mailbox.Maildir (calls os.listdir)?

https://hg.python.org/cpython/file/3.4/Lib/mailbox.py#l408

I think there are plenty of virtual containers that fail your rule.

-Scott

-- 
Scott Dial
scott at scottdial.com


More information about the Python-ideas mailing list