Python源文件组织
我正在开始一个新的 Python 项目,并且希望尽可能严格地遵循标准约定。例如,我读过导入语句应该放在第一位。但我还没有找到任何约定,例如将所有函数定义放在所有类定义之前或之后。对于这样的事情有什么约定吗?或者每个人通常只是按照似乎有意义的顺序组织函数和类定义之类的东西?
I'm starting a new Python project, and want to follow standard conventions as closely as possible. I've read that import statements should come first, for example. But I haven't found any conventions for things like putting all function definitions before or after all class definitions. Are there any conventions for things like this? Or does everyone typically just organize things like function and class definitions in whatever order seems to make sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PEP8 是 Python 风格指南:http://www.python.org/dev/peps /pep-0008/
导入位于文件顶部,但允许方法级导入。
类和函数没有特定的顺序。使用有意义的东西。
PEP8 is the Python style guide: http://www.python.org/dev/peps/pep-0008/
Imports come at the top of the file, though method-level imports are allowed.
There's no specific ordering to classes and functions. Use what makes sense.
不,没有函数和类的组织约定。但是,有一些基本准则可以使您的源代码流程更好,并且对读者更有意义:
有关如何完成此操作的示例,只需查看 Python 标准库即可。某些模块的源文件应该能让您了解 Python 中源代码的组织方式。
No, there's no convention for organization of functions and classes. However, there are some basic guidelines that will make your source flow better and make more sense to readers:
For examples on how this is done, look no further than the Python standard library. The source files from some of the modules should give you an idea how source is organized in Python.