Python /其他语言中跨模块的模块中的全局数据是如何管理的?(How does/should global data in modules across packages be managed i

编程入门 行业动态 更新时间:2024-10-27 15:22:01
Python /其他语言中跨模块的模块中的全局数据是如何管理的?(How does/should global data in modules across packages be managed in Python/other languages?)

我正在设计一个可以编译和解释的编程语言( 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?

最满意答案

那么,你问了很多问题。 以下是一些提示,以便进一步了解:

一个。 Python代码是lexed并编译成Python特定的指令,但没有编译为机器可执行代码。 每当您运行与现有.pyc时间戳不匹配的python代码时,都会自动创建“.pyc”文件。 此功能可以关闭。 您可以使用dis模块来查看这些说明。 湾 当一个模块被导入时,它将在其自己的命名空间中执行(从上到下),并且该命名空间全局缓存。 从另一个模块导入时,模块不会再次执行。 请记住,def只是一个声明。 你可能想在你的代码中打印('编译这个模块')语句来追踪它。

这取决于。

最近有增强功能,主要是指定需要加载哪个模块。 模块可以有相对路径,这样一个巨大的项目可能会有多个模块具有相同的名称。

Python本身不适用于编译语言。 谷歌为“空中吞下博客”,看到试图加快“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.

更多推荐

本文发布于:2023-07-31 01:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340423.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   全局   语言   数据   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!