python - Python 中的文件/模块结构

标签 python module package

所以我才刚刚开始使用 Python,目前正在努力通过 http://diveintopython3.ep.io/ .代码示例很好,但其中绝大多数都是四行的小片段,我想多看一点大图。

据我所知——如果我错了请纠正我——每个“.py”文件变成一个“模块”,目录中的一组模块变成一个“包”(至少,它确实如此)如果我在该目录中创建一个 __init__.py 文件)。如果我没有 __init__.py 文件怎么办?

那么每个“模块”文件是什么样的呢?我通常只在文件中定义一个类吗?除了类定义和一些 import 命令之外,该文件中还有其他内容吗?

最佳答案

What is it if I don't have a __init__.py file?

这只是一个文件夹。

Do I generally define only one class in the file?

这取决于。不一定。

Does anything else go in that file besides the class definition and maybe a handful of import commands?

你可以放任何你想放的东西。任何至少是有效的 python。

关于python - Python 中的文件/模块结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4683955/

相关文章:

python - 类型错误 : unsupported operand type(s) for -: 'tuple' and 'tuple'

python - 在 pypi 上给定一个模块,有没有办法反省模块并显示所有依赖项?

javascript - 是否有与 PHP 的 include 等效的 Node.js,以便包含的代码可以访问父文件的变量?

r - R 包中的双语(英语和葡萄牙语)文档

python - Flask SQLAlchemy 不关闭 MySQL 数据库连接

python - 根据不同的数组对随机生成的 Numpy 数组进行排序

python - 在 Django 模型中实现依赖选择的最佳方式

python - 在虚拟环境中安装我自己的 python 模块

java - 安装外部 java 包

java - 如何使用独立于目录结构的包名?