lazy_loader attach_stub一种变体玩法

此方法在scikit-image包中可以明显看到使用

玩法流程

  • __init__.py直接基于attach_stub 进行定义懒加载,以后的使用就同时标准玩法了
  • 添加__init__.pyi 进行显示的引入定义,方便实现类型检查以及ide 的自动提示

一个参考玩法

  • __init__.py 定义
import lazy_loader as lazy
__getattr__, __dir__, __all__ = lazy.attach_stub(__name__, __file__)
  • __init__.pyi 定义
__all__ = ['applatform_login','platform_login']
 
from . import platform_login
from .platform_login import applatform_login
  • 使用效果

可以看到ide 包含了自动提示能力

说明

通过lazy_loader 的attach_stub ,同时能提供懒加载的能力,还能帮助我们进行类型提示,是比较值得使用的玩法

参考资料

https://github.com/scikit-image/scikit-image/blob/main/skimage/filters/init.py
https://github.com/scikit-image/scikit-image/blob/main/skimage/filters/init.pyi
https://github.com/scientific-python/lazy-loader/blob/main/lazy_loader/init.py#L304

posted on   荣锋亮  阅读(8)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-10-04 dremio 24.2 新增druid arp 扩展
2023-10-04 dremio 24.2 社区版发布
2023-10-04 apache hop kettle 替换方案
2022-10-04 graylog OutputRouter 简单说明
2022-10-04 graylog MessageOutput 简单说明
2022-10-04 graylog OutputBufferProcessor 简单说明
2022-10-04 graylog OutputBuffer 简单说明

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示