延迟导入Python模块的几种方法

延迟导入Python模块的几种方法 - 知乎 (zhihu.com)

# __init__.py

import importlib

__all__ = ['complicated']

def __getattr__(name):
  if name in __all__:
    return importlib.import_module("." + name, __name__)
  else:
    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

def __dir__():
  return __all__

 

posted @ 2023-10-23 11:11  你说夕阳很美  阅读(70)  评论(0)    收藏  举报