Python模块可以由多个文件组成吗?

huangapple go评论98阅读模式
英文:

Can a Python module be multiple files?

问题

多年来,我一直知道Python模块的定义是作为一个单独的文件。事实上,甚至官方文档也说明“模块是包含Python定义和语句的文件”。然而,这个来自看起来相当有经验的人的在线教程却表示“模块通常对应一个单独的文件”。这里的“通常”是从哪里来的呢?Python模块可以由多个文件组成吗?

英文:

For years, I've known that the very definition of a Python module is as a separate file. In fact, even the official documentation states that "a module is a file containing Python definitions and statements". Yet, this online tutorial from people who seem pretty knowledgeable states that "a module usually corresponds to a single file". Where does the "usually" come from? Can a Python module consist of multiple files?

答案1

得分: 2

不是真的。请不要对一个短小的丢弃句子的措辞读得太多,这个句子出现在一个更大的博客文章中,该文章涉及包和包装,它们都本质上是多文件的。

导入不会使模块成为多文件

按照模块因导入而成为多文件的逻辑... 几乎任何 Python 模块都是多文件的。除非导入来自子树,这对使用该模块的代码没有实际可识别的区别。顺便说一句,子树导入的概念与 Python 包相关。

__module__,出现在类和函数上的属性,也映射到一个文件,由导入路径确定。

扩展模块定义的有用性似乎有限,而且可能会引发混淆。让导入保持导入,让模块保持模块(即文件)。

但这只是我的个人观点。

让我们在语言上严格解释一下

并参考 Python 教程。我认为他们在某个时候会谈论模块,并且在措辞上会比一个主要关注其他主题的博客文章更加小心。

6. 模块

为了支持这一点,Python 有一种方法可以将定义放在一个文件中,并在脚本或交互式解释器实例中使用它们。这样的文件称为模块;模块中的定义可以被导入到其他模块或主模块(在顶层执行的脚本和计算器模式中可以访问的变量集合)中。

模块是一个包含 Python 定义和语句的文件。文件名就是模块名,附加了 .py 后缀。在一个模块内,模块的名称(作为字符串)可以作为全局变量 name 的值获得。

另外,如果不称之为模块,而称之为文件呢?这假定你将 Python 代码存储在文件系统中。但你也可以有一个将其存储在数据库中的特殊环境,或者将其嵌入到较大的 C/Rust 可执行文件中。因此,模块 更好地被理解为“连续的 Python 代码块”。通常情况下,这是一个文件,但有一个单独的术语允许更灵活,而不改变核心概念。

英文:

Not really.

Don't read too much into the phrasing of one short throwaway sentence, in a much larger blog post that concerns packaging and packages, both of which are by nature multi-file.

Imports do not make modules multifile

By the logic that modules are multifile because of imports... almost any python module is multifile. Unless the imports are from the subtree, which has no real discernible difference to code using the module. That notion of subtree imports, btw, is relevant... to Python packages.

__module__, the attribute found on classes and functions, also maps to one file, as determined by import path.

The usefulness of expanding the definition of modules that way seems… limited, and risks confusion. Let imports be imports ans modules be modules (i.e. files).

But that's like, my personal opinion.

Let's go all language lawyer on it

And refer to the Python tutorial. I figure they will be talking about modules at some point and will be much more careful in their wording than a blog post which was primarily concerned about another subject.

6. Modules

>To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).
>
>A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable name.

p.s. OK, what about calling it a file, instead of a module, then?

That supposes that you store Python code in a file system. But you could have an exotic environment that stores it in a database instead (or embeds it in a larger C/Rust executable?). So, module, seems better understood as a "contiguous chunk of Python code". Usually that's a file, but having a separate term allows for flexibility, without changing anything to the core concepts.

答案2

得分: 1

是的,当然。
如果你在PATH或与你的脚本相同的目录中创建一个名为mylib的文件夹,你就可以使用import mylib
确保在该文件夹中放置__init__.py文件,并在其中从其他文件导入一切,因为变量、函数等只从__init__.py中导入。

例如:

project -+- lib -+- __init__.py
         |       +- bar.py
         |       +- bar2.py
         |
         +- foo.py

__init__.py:

from bar import test, random
from bar2 import sample

foo.py:

import lib

print(test)
sample()

希望对你有所帮助。

英文:

Yes, sure.
If you create a folder named mylib in your PATH or in the same directory as your script, it allows you to use import mylib.
Make sure to put __init__.py in the folder and in that imoprt everything from other files because the variables, functions, etc. import just from the __init__.py.

For example:

project -+- lib -+- __init__.py
         |       +- bar.py
         |       +- bar2.py
         |
         +- foo.py

__init__.py :

from bar import test, random
from bar2 import sample

foo.py :

import lib

print(test)
sample()

Hope it helps.

答案3

得分: 0

是的,一个Python模块可以包含多个文件。基本上,你会得到一个用于模块主要代码的文件,并在该主文件中包含其他一些你可以使用的工具。

例如,你可以有一个文件叫做my_splitter_module.py,在其中你有……比如说一个函数,它接收一个整数列表并将其分成两半,创建两个列表。现在假设你想要将第一半中的所有数字相乘([1, 2, 3] -> 1 * 2 * 3),而将另一半中的数字相加([1, 2, 3] -> 1 + 2 + 3)。现在假设你不想让代码变得混乱,所以你决定再写另外两个函数,一个用于获取列表并将其中的项相乘,另一个用于将它们相加。

当然,你可以将这两个函数都放在同一个my_splitter_module.py文件中,但在其他情况下,当你有大文件和大类等时,你可能会希望创建像multiply_list.py和sum_list.py这样的文件,然后将它们导入到my_splitter_module.py中。

最后,你会在你的main.py文件中导入my_splitter_module.py,在这个过程中,你也会导入multiply_list.py和sum_list.py文件。

英文:

Yup, a python module can include more than one file. Basically what you would do is get a file for the main code of the module you are writing, and in that main file include some other tools you can use.

For example, you can have the file my_splitter_module.py, in which you have... say a function that gets a list of integers and split it in half creating two lists. Now say you wanna multiply all the numbers that are in the first half between each other ([1, 2, 3] -> 1 * 2 * 3), but with the other half sum them ([1, 2, 3] -> 1 + 2 + 3). Now say you don't want to make the code messy and so you decide to make another two functions, one that gets a list and multiply its items, and another that sum them.

Of course, you could make the two functions in the same my_splitter_module.py file, but in other situations when you have big files with big classes etc, you would like to make a file like multiply_list.py and sum_list.py, and then importing them to the my_splitter_module.py

At the end, you would import my_splitter_module.py to your main.py file, and while doing this, you would also be importing multiply_list.py and sum_list.py files.

huangapple
  • 本文由 发表于 2023年2月7日 01:52:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定