2018-06-27 9:36 GMT+02:00 Michael Selik <mike@selik.org>:
On Tue, Jun 26, 2018, 11:43 PM Jacco van Dorp <j.van.dorp@deonet.nl> wrote:
2018-06-26 17:34 GMT+02:00 Franklin? Lee <leewangzhong+python@gmail.com>:
Caller detects: The caller checks length before calling the dunder. If there is no dunder, it doesn't check. Are there real-world cases where length is not defined on an iterable collection?
Generators dont have a __len__ method. And they might have min/max that can be calculated without iterating over the entire thing. The builtin range() is an example. (but also an exception, since it does have a __len__ attribute. This is specifically part of range and not generators in general, though.).
However, range() is an example where the dunders could be valuable - max(range(1e7)) already takes noticable time here, while it's rather easy to figure it out from start stop and step, just like len now does for it.
Have you ever written ``max(range(x))`` in production code?
Have you ever written len(range(x)) in production code ?