在Python/其他语言中如何/应该如何管理跨包的模块中的全局数据?
我正在尝试为一种可以编译的编程语言(Heron)设计包和模块系统并进行了解释,从我所看到的来看,我真的很喜欢 Python 方法。 Python 有丰富的模块可供选择,这似乎在很大程度上促成了它的成功。
我不知道的是,如果一个模块包含在两个不同的编译包中,Python 中会发生什么:数据是否有单独的副本,或者是共享的?
与此相关的是一系列附带问题:
- 我假设包可以在 Python 中编译是正确的吗?
- 这两种方法(复制或共享模块数据)有何优缺点?
- 从 Python 社区的角度来看,Python 模块系统是否存在众所周知的问题?例如,是否正在考虑使用 PEP 来增强模块/包?
- Python 模块/包系统的某些方面是否不适用于编译语言?
I am trying to design the package and module system for a programming language (Heron) which can be both compiled and interpreted, and from what I have seen I really like the Python approach. Python has a rich choice of modules, which seems to contribute largely to its success.
What I don`t know is what happens in Python if a module is included in two different compiled packages: are there separate copies made of the data or is it shared?
Related to this are a bunch of side-questions:
- Am I right in assuming that packages can be compiled in Python?
- What are there pros and cons to the two approaches (copying or sharing of module data)?
- Are there widely known problems with the Python module system, from the point of view of the Python community? For example is there a PEP under consideration for enhancing modules/packages?
- Are there certain aspects of the Python module/package system which wouldn`t work well for a compiled language?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,你问了很多问题。以下是一些进一步了解的提示:
a. Python 代码被词法分析并编译为 Python 特定指令,但不会编译为机器可执行代码。每当您运行与现有 .pyc 时间戳不匹配的 python 代码时,都会自动创建“.pyc”文件。该功能可以关闭。您可以使用 dis 模块来查看这些说明。
b.当导入模块时,它会在自己的命名空间中执行(从上到下),并且该命名空间会全局缓存。当您从另一个模块导入时,该模块不会再次执行。请记住 def 只是一个声明。您可能需要在代码中添加 print('compiling this module') 语句来跟踪它。
这取决于。
最近有一些增强功能,主要是围绕指定需要加载的模块。模块可以具有相对路径,因此一个巨大的项目可能有多个同名的模块。
Python 本身不适用于编译语言。谷歌搜索“unladen咽下博客”,看看尝试加速一种语言的艰辛,其中“a = sum(b)”可以改变执行之间的含义。除了极端情况之外,模块系统在源代码和编译的库系统之间架起了一座良好的桥梁。该方法效果很好,Python 轻松包装 C 代码(swig 等)也很有帮助。
Well, you asked a lot of questions. Here are some hints to get a bit further:
a. Python code is lexed and compiled into Python specific instructions, but not compiled to machine executable code. The ".pyc" file is automatically created whenever you run python code that does not match the existing .pyc timestamp. This feature can be turned off. You might play with the dis module to see these instructions.
b. When a module is imported, it is executed (top to bottom) in its own namespace and that namespace cached globally. When you import from another module, the module is not executed again. Remember that def is just a statement. You may want to put a print('compiling this module') statement in your code to trace it.
It depends.
There were recent enhancements, mostly around specifying which module needed to be loaded. Modules can have relative paths so that a huge project might have multiple modules with the a same name.
Python itself won't work for a compiled language. Google for "unladen swallow blog" to see the tribulations of trying to speed up a language where "a = sum(b)" can change meanings between executions. Outside of corner cases, the module system forms a nice bridge between source code and a compiled library system. The approach works well, and Python's easy wrapping of C code (swig, etc.) helps.
模块是 Python 中唯一真正的全局对象,所有其他全局数据都基于模块系统(使用 sys.modules 作为注册表)。包只是具有用于导入子模块的特殊语义的模块。将 .py 文件“编译”为 .pyc 或 .pyo 并不是大多数语言所理解的编译:它仅检查语法并创建一个代码对象,该代码对象在解释器中执行时会创建模块对象。
example.py:
交互式会话:
您的问题:
is
运算符和id()
)之外,应该与共享没有区别。mungepath/__init__.py:
交互式会话:
Modules are the only truly global objects in Python, with all other global data based around the module system (which uses sys.modules as a registry). Packages are simply modules with special semantics for importing submodules. "Compiling" a .py file into a .pyc or .pyo isn't compilation as understood for most languages: it only checks the syntax and creates a code object which, when executed in the interpreter, creates the module object.
example.py:
Interactive session:
Your questions:
is
operator andid()
in Python).mungepath/__init__.py:
Interactive session:
全局数据的范围在解释器级别。
Global data is scoped at the interpreter level.