python-more-itertools/0002-more.py-document-ilen.patch
zhang-liang-pengkun b4b843ff79 more.py: document ilen
Signed-off-by: zhang-liang-pengkun <zhangliangpengkun@xfusion.com>
2023-11-24 12:21:37 +08:00

30 lines
898 B
Diff

From fc39d84358199cd0fc7a33e9179244a4987e36c7 Mon Sep 17 00:00:00 2001
From: Ben Mintz <bmintz@protonmail.com>
Date: Mon, 7 May 2018 03:29:41 -0500
Subject: [PATCH] more.py: document ilen
I found the implementation of ilen to be confusing, so I documented it.
---
more_itertools/more.py | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/more_itertools/more.py b/more_itertools/more.py
index 6de5b03..a5fd960 100644
--- a/more_itertools/more.py
+++ b/more_itertools/more.py
@@ -409,7 +409,11 @@ def ilen(iterable):
This consumes the iterable, so handle with care.
"""
+ # maxlen=1 only stores the last item in the deque
d = deque(enumerate(iterable, 1), maxlen=1)
+ # since we started enumerate at 1,
+ # the first item of the last pair will be the length of the iterable
+ # (assuming there were items)
return d[0][0] if d else 0
--
2.39.0.windows.2