Skip to content

Instantly share code, notes, and snippets.

@ynkdir
Created May 6, 2025 07:27
Show Gist options
  • Save ynkdir/8c243ce2c2a8ccedbc12127897187678 to your computer and use it in GitHub Desktop.
Save ynkdir/8c243ce2c2a8ccedbc12127897187678 to your computer and use it in GitHub Desktop.
import importlib
import inspect
class LazyImport:
def __init__(self, module):
self._module = module
def __getattr__(self, name):
try:
value = getattr(self._module, name)
if inspect.ismodule(value):
return LazyImport(value)
return value
except AttributeError:
pass
try:
return LazyImport(importlib.import_module(self._module.__name__ + "." + name))
except ImportError:
raise AttributeError(f"module '{self._module.__name__}' has no attribute '{name}'") from None
# in win32more/__init__.py
# lazy = LazyImport(sys.modules[__name__])
# then
# from win32more import lazy as win32more
# win32more.Windows.Win32.UI.WindowsAndMessaging.MessageBox(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment