Skip to content

Commit dffb909

Browse files
authored
Speed-up lazy heapq import in collections (gh-127538)
1 parent bfb0788 commit dffb909

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Lib/collections/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
except ImportError:
6060
pass
6161

62+
heapq = None # Lazily imported
63+
6264

6365
################################################################################
6466
### OrderedDict
@@ -633,7 +635,10 @@ def most_common(self, n=None):
633635
return sorted(self.items(), key=_itemgetter(1), reverse=True)
634636

635637
# Lazy import to speedup Python startup time
636-
import heapq
638+
global heapq
639+
if heapq is None:
640+
import heapq
641+
637642
return heapq.nlargest(n, self.items(), key=_itemgetter(1))
638643

639644
def elements(self):

0 commit comments

Comments
 (0)