
昨天突然有的想法,然后试验可行以后就立即做出来了。这个库不仅仅控制 __all__ 变量,而且严格控制导出的成员,未导出的成员则获取不到。
例子:
# mymodule.py from modul import exports @exports def foo(): return 42 baz = "unexported" bar = "hello" exports.bar = bar 调用:
>>> import mymodule >>> mymodule.foo() 42 >>> mymodule.bar "hello" >>> mymodule.baz AttributeError: Module 'test' has no attribute 'baz' >>> mymodule.__all__ ['foo', 'bar'] 除此之外还支持所有 dict API 以及 modul.exports = ... 整体赋值。
作为一个玩具项目,也不知会不会有什么用。
1 jaredyam 2022 年 5 月 19 日 效率真高。大佬如何理解包级暴露、模块级暴露、对象级暴露的差异? |
3 abersheeran 2022 年 5 月 19 日 感觉没有什么实际使用的场景,不过挺有意思的。 |