如果您想对用户隐藏实施细节,可以采用两种方法。第一个使用约定来表示什么是公共 API 的一部分和不是公共 API 的一部分,另一个是 hack。
在 python 库中声明 API 的约定是将所有应该公开的类/函数/名称添加到最顶层 __init__.py 的 __all__-list 中。它并没有做那么多有用的事情,它现在的主要目的是象征性的“请使用这个,别无其他”。你的可能看起来像这样:
urdu/urdu/__init__.py
from urdu.stemmer import Foo, Bar, Baz
__all__ = [Foo, Bar, Baz]
为了强调这一点,您还可以在名称前给出stemmerUtil.py an underscore 内的所有定义,例如def privateFunc(): ... 变为 def _privateFunc(): ...
但您也可以通过将代码设置为资源而不是包中的模块并动态加载来对解释器隐藏代码。这是一个 hack,可能是个坏主意,但在技术上是可行的。
首先,您将 stemmerUtil.py 重命名为 stemmerUtil - 现在它不再是 python 模块并且不能使用 import 关键字导入。接下来,更新stemmer.py中的这一行
import stemmerUtil
与
import importlib.util
import importlib.resources
# in python3.7 and lower, this is importlib_resources and needs to be installed first
stemmer_util_spec = importlib.util.spec_from_loader("stemmerUtil", loader=None)
stemmerUtil = importlib.util.module_from_spec(stemmer_util_spec)
with importlib.resources.path("urdu", "stemmerUtil") as stemmer_util_path:
with open(stemmer_util_path) as stemmer_util_file:
stemmer_util_code = stemmer_util_file.read()
exec(stemmer_util_code, stemmerUtil.__dict__)
运行此代码后,您可以像导入它一样使用 stemmerUtil 模块,但安装您的软件包的任何人都看不到它 - 除非他们也运行此确切代码。
但正如我所说,如果您只是想与您的用户沟通包的哪一部分是公共 API,那么第一个解决方案是非常可取的。