Skip to content

Commit 7258dbc

Browse files
authored
Use lazy imports in collections (gh-145054)
1 parent e1bd0cd commit 7258dbc

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

Lib/collections/__init__.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
_sys.modules['collections.abc'] = _collections_abc
3333
abc = _collections_abc
3434

35+
lazy from copy import copy as _copy
36+
lazy from heapq import nlargest as _nlargest
3537
from itertools import chain as _chain
3638
from itertools import repeat as _repeat
3739
from itertools import starmap as _starmap
@@ -59,8 +61,6 @@
5961
except ImportError:
6062
pass
6163

62-
heapq = None # Lazily imported
63-
6464

6565
################################################################################
6666
### OrderedDict
@@ -634,12 +634,7 @@ def most_common(self, n=None):
634634
if n is None:
635635
return sorted(self.items(), key=_itemgetter(1), reverse=True)
636636

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

644639
def elements(self):
645640
'''Iterator over elements repeating each as many times as its count.
@@ -1249,11 +1244,10 @@ def __copy__(self):
12491244
def copy(self):
12501245
if self.__class__ is UserDict:
12511246
return UserDict(self.data.copy())
1252-
import copy
12531247
data = self.data
12541248
try:
12551249
self.data = {}
1256-
c = copy.copy(self)
1250+
c = _copy(self)
12571251
finally:
12581252
self.data = data
12591253
c.update(self)

0 commit comments

Comments
 (0)