From bbe2f60b3c19ecaa02ca07be14474eaacfcb59a0 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Thu, 1 Mar 2012 16:26:35 +0100 Subject: Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying. Moreover, the implementation is now O(1) rather than O(n). Thanks to Yury Selivanov for reporting. --- Lib/_weakrefset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Lib/_weakrefset.py') diff --git a/Lib/_weakrefset.py b/Lib/_weakrefset.py index 42653699a3..f34aa864bc 100644 --- a/Lib/_weakrefset.py +++ b/Lib/_weakrefset.py @@ -63,7 +63,7 @@ class WeakSet: yield item def __len__(self): - return sum(x() is not None for x in self.data) + return len(self.data) - len(self._pending_removals) def __contains__(self, item): try: -- cgit v1.2.1