diff options
author | Victor Stinner <vstinner@python.org> | 2020-06-17 17:10:47 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-17 17:10:47 (GMT) |
commit | 7824cc05bfe7f8181b21848a52007ddaf5612b9b (patch) | |
tree | 27139e642b2e07807a5ff6a9194ea1c6762247e5 /Lib/collections/__init__.py | |
parent | bb6ec14479f18c32e71e43f2785f177aa17aabbd (diff) | |
download | cpython-7824cc05bfe7f8181b21848a52007ddaf5612b9b.zip cpython-7824cc05bfe7f8181b21848a52007ddaf5612b9b.tar.gz cpython-7824cc05bfe7f8181b21848a52007ddaf5612b9b.tar.bz2 |
bpo-41006: collections imports lazily heap (GH-20940)
The collections module now imports lazily the heapq modules in the
Counter.most_common() method to speedup Python startup time.
Diffstat (limited to 'Lib/collections/__init__.py')
-rw-r--r-- | Lib/collections/__init__.py | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 42d0ec7..5d75501 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -27,7 +27,6 @@ __all__ = [ ] import _collections_abc -import heapq as _heapq import sys as _sys from itertools import chain as _chain @@ -608,7 +607,10 @@ class Counter(dict): # Emulate Bag.sortedByCount from Smalltalk if n is None: return sorted(self.items(), key=_itemgetter(1), reverse=True) - return _heapq.nlargest(n, self.items(), key=_itemgetter(1)) + + # Lazy import to speedup Python startup time + import heapq + return heapq.nlargest(n, self.items(), key=_itemgetter(1)) def elements(self): '''Iterator over elements repeating each as many times as its count. |