https://github.com/python/cpython/commit/dffb90911a585a0921664c8b1c229d0883e65ee7
commit: dffb90911a585a0921664c8b1c229d0883e65ee7
branch: main
author: Raymond Hettinger <[email protected]>
committer: rhettinger <[email protected]>
date: 2024-12-02T20:45:36-06:00
summary:

Speed-up lazy heapq import in collections (gh-127538)

files:
M Lib/collections/__init__.py

diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index d688141f9b183d..78229ac54b80da 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -59,6 +59,8 @@
 except ImportError:
     pass
 
+heapq = None  # Lazily imported
+
 
 
################################################################################
 ### OrderedDict
@@ -633,7 +635,10 @@ def most_common(self, n=None):
             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):

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to