Python libraries often need to maintain a stable public API even as internal implementations evolve, gain new backends, or depend on heavy optional libraries. In Python, where internal objects are easy to inspect and import, users can come to rely on "reachable internals" that were never intended to be public, making refactoring risky and slowing long-term maintenance. This paper revisits the pointer-to-implementation (PIMPL) idiom from C++ and reinterprets it as a Pythonic pattern of opaque delegation: a small public object (or module) that delegates its behavior to a separate implementation object treated as internal. We situate this pattern within a broader taxonomy of encapsulation techniques in Python, relate it to existing practices such as module-level indirection, facade objects, and backend dispatch, and identify PIMPL-like structures already used in the standard library and the scientific Python ecosystem. We then show how a Pythonic PIMPL can be used in existing codebases to isolate heavy dependencies, support lazy imports, and enable runtime selection of alternative backends without changing the public API. Finally, we discuss the benefits and trade-offs of the approach and offer practical guidance on when the pattern is appropriate and how to apply it in large, long-lived Python libraries.


翻译:Python库通常需要在内部实现演进、新增后端支持或依赖重型可选库时,仍保持稳定的公共API。在Python中,由于内部对象易于检查和导入,用户可能依赖那些本不应公开的"可访问内部实现",这使得重构存在风险并拖慢长期维护进程。本文重新审视C++中的指针到实现(PIMPL)惯用法,将其重新诠释为一种Pythonic的不透明委托模式:即通过小型公共对象(或模块)将其行为委托给被视为内部实现的独立对象。我们将该模式置于更广泛的Python封装技术分类体系中,关联到模块级间接调用、门面对象和后端调度等现有实践,并识别标准库及科学Python生态中已使用的类PIMPL结构。随后展示如何将Pythonic PIMPL应用于现有代码库,以隔离重型依赖、支持延迟导入,并在不改变公共API的前提下实现运行时替代后端的选择。最后,我们讨论该方法的优势与权衡,并就该模式的适用场景及其在大型长期维护Python库中的实施提供实践指导。

0
下载
关闭预览

相关内容

Python是一种面向对象的解释型计算机程序设计语言,在设计中注重代码的可读性,同时也是一种功能强大的通用型语言。
【2022新书】Python DevOps,245页pdf
专知会员服务
91+阅读 · 2022年7月11日
【2021新书】面向对象的Python编程,418页pdf
专知会员服务
71+阅读 · 2021年12月15日
【开放书】Python + Matplotlib可视化指南,249页pdf
专知会员服务
98+阅读 · 2021年11月17日
【干货书】Python参考手册,210页pdf
专知会员服务
64+阅读 · 2021年4月30日
专知会员服务
92+阅读 · 2021年4月12日
【书籍推荐】简洁的Python编程(Clean Python),附274页pdf
专知会员服务
183+阅读 · 2020年1月1日
吐血整理!140种Python标准库、第三方库和外部工具都有了
炼数成金订阅号
16+阅读 · 2019年7月30日
一个牛逼的 Python 调试工具
机器学习算法与Python学习
15+阅读 · 2019年4月30日
实战 | 用Python做图像处理(一)
七月在线实验室
25+阅读 · 2018年5月23日
这可能是学习Python最好的免费在线电子书
程序猿
56+阅读 · 2018年5月17日
干货 | Python 爬虫的工具列表大全
机器学习算法与Python学习
11+阅读 · 2018年4月13日
福利 | 当Python遇上大数据与机器学习,入门so easy!
用Python制作3D动画
Python程序员
30+阅读 · 2018年1月17日
Python 3 尴尬了这么久,终于有救了
AI100
13+阅读 · 2017年11月18日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
4+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
5+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
1+阅读 · 2014年12月31日
国家自然科学基金
3+阅读 · 2014年12月31日
VIP会员
相关资讯
吐血整理!140种Python标准库、第三方库和外部工具都有了
炼数成金订阅号
16+阅读 · 2019年7月30日
一个牛逼的 Python 调试工具
机器学习算法与Python学习
15+阅读 · 2019年4月30日
实战 | 用Python做图像处理(一)
七月在线实验室
25+阅读 · 2018年5月23日
这可能是学习Python最好的免费在线电子书
程序猿
56+阅读 · 2018年5月17日
干货 | Python 爬虫的工具列表大全
机器学习算法与Python学习
11+阅读 · 2018年4月13日
福利 | 当Python遇上大数据与机器学习,入门so easy!
用Python制作3D动画
Python程序员
30+阅读 · 2018年1月17日
Python 3 尴尬了这么久,终于有救了
AI100
13+阅读 · 2017年11月18日
相关基金
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
4+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
5+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
1+阅读 · 2014年12月31日
国家自然科学基金
3+阅读 · 2014年12月31日
Top
微信扫码咨询专知VIP会员