From 00ae76a61524dc0c68a91df3a2cd61969af9fec2 Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye Date: Sun, 5 Jul 2020 04:02:10 +0000 Subject: [PATCH 1/3] Add example on lazy imports --- Doc/library/importlib.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst index 99bfeacbbc7407..a8e48e51f77a72 100644 --- a/Doc/library/importlib.rst +++ b/Doc/library/importlib.rst @@ -1719,6 +1719,27 @@ To import a Python source file directly, use the following recipe spec.loader.exec_module(module) +Implementing lazy imports +''''''''''''''''''''''''' + +The example below shows how to implement lazy imports:: + + import sys + import importlib.util + def lazy_import(name): + spec = importlib.util.find_spec(name) + loader = importlib.util.LazyLoader(spec.loader) + spec.loader = loader + module = importlib.util.module_from_spec(spec) + sys.modules[name] = module + loader.exec_module(module) + return module + + >>> lazy_typing = lazy_import("typing") + >>> lazy_typing.TYPE_CHECKING + False + + Setting up an importer '''''''''''''''''''''' From 29396dc99eef0d688745501c91ed336c7135f56f Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye Date: Tue, 7 Jul 2020 01:06:52 +0000 Subject: [PATCH 2/3] Use four spaces for indentation --- Doc/library/importlib.rst | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst index a8e48e51f77a72..3e6cd0fd8b113d 100644 --- a/Doc/library/importlib.rst +++ b/Doc/library/importlib.rst @@ -1727,13 +1727,13 @@ The example below shows how to implement lazy imports:: import sys import importlib.util def lazy_import(name): - spec = importlib.util.find_spec(name) - loader = importlib.util.LazyLoader(spec.loader) - spec.loader = loader - module = importlib.util.module_from_spec(spec) - sys.modules[name] = module - loader.exec_module(module) - return module + spec = importlib.util.find_spec(name) + loader = importlib.util.LazyLoader(spec.loader) + spec.loader = loader + module = importlib.util.module_from_spec(spec) + sys.modules[name] = module + loader.exec_module(module) + return module >>> lazy_typing = lazy_import("typing") >>> lazy_typing.TYPE_CHECKING From 782746c7d98357c5608ddcefc4ac516439888e73 Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye Date: Tue, 7 Jul 2020 02:33:40 +0000 Subject: [PATCH 3/3] change to console --- Doc/library/importlib.rst | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst index 3e6cd0fd8b113d..f7286d2ade8bd1 100644 --- a/Doc/library/importlib.rst +++ b/Doc/library/importlib.rst @@ -1724,20 +1724,22 @@ Implementing lazy imports The example below shows how to implement lazy imports:: - import sys - import importlib.util - def lazy_import(name): - spec = importlib.util.find_spec(name) - loader = importlib.util.LazyLoader(spec.loader) - spec.loader = loader - module = importlib.util.module_from_spec(spec) - sys.modules[name] = module - loader.exec_module(module) - return module - - >>> lazy_typing = lazy_import("typing") - >>> lazy_typing.TYPE_CHECKING - False + >>> import importlib.util + >>> import sys + >>> def lazy_import(name): + ... spec = importlib.util.find_spec(name) + ... loader = importlib.util.LazyLoader(spec.loader) + ... spec.loader = loader + ... module = importlib.util.module_from_spec(spec) + ... sys.modules[name] = module + ... loader.exec_module(module) + ... return module + ... + >>> lazy_typing = lazy_import("typing") + >>> #lazy_typing is a real module object, + >>> #but it is not loaded in memory yet. + >>> lazy_typing.TYPE_CHECKING + False