diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2024-12-03 02:45:36 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-03 02:45:36 (GMT) |
commit | dffb90911a585a0921664c8b1c229d0883e65ee7 (patch) | |
tree | 974cb3e36bc4b71ad4182c335558e608442fefdc | |
parent | bfb0788bfcaab7474c1be0605552744e15082ee9 (diff) | |
download | cpython-dffb90911a585a0921664c8b1c229d0883e65ee7.zip cpython-dffb90911a585a0921664c8b1c229d0883e65ee7.tar.gz cpython-dffb90911a585a0921664c8b1c229d0883e65ee7.tar.bz2 |
Speed-up lazy heapq import in collections (gh-127538)
-rw-r--r-- | Lib/collections/__init__.py | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index d688141..78229ac 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -59,6 +59,8 @@ try: except ImportError: pass +heapq = None # Lazily imported + ################################################################################ ### OrderedDict @@ -633,7 +635,10 @@ class Counter(dict): return sorted(self.items(), key=_itemgetter(1), reverse=True) # Lazy import to speedup Python startup time - import heapq + global heapq + if heapq is None: + import heapq + return heapq.nlargest(n, self.items(), key=_itemgetter(1)) def elements(self): |