Python文件的常见头格式是什么? [英] What is the common header format of Python files?

查看:819
本文介绍了Python文件的常见头格式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在有关Python编码指南的文档中遇到了Python源文件的以下头文件格式:

 #!/ usr / bin / env python 

foobar.py:foobar的说明。

__author__ =Barack Obama
__copyright__ =版权所有2009,地球

这是Python世界中标题的标准格式吗?
我可以在标题中放置什么其他字段/信息?
Python gurus分享您的Python源标头的指南: - )

解决方案

$ c> foobar 模块。



第一个是模块的 docstring ,这已在彼得的回答中解释过。 / p>


如何组织我的模块(源文件)? (存档)



每个文件的第一行是#!/ usr / bin / env python 这使得可以作为脚本以隐式方式调用解释器来运行文件,例如 。如果描述很长,则会显示一个描述。



所有代码,包括import语句,都应该遵循文档字符串。否则,解释器不会识别文档字符串,并且您不能在交互式会话中访问它(即通过 obj .__ doc __



首先导入内置模块,然后是第三方模块,随后对路径和您自己的模块进行任何更改。特别是,您的模块的路径和名称的添加可能会快速变化:将它们放在一个地方可以更容易找到。



  __ author__ =Rob Knight,Gavin Huttley和Peter Maxwell
__copyright__ =版权所有2007,The Cogent Project
__credits__ = [Rob Knight,Peter Maxwell,Gavin Huttley,
Matthew Wakefield]
__license__ =GPL
__version__ =1.0.1
__maintainer__ =Rob Knight
__email__ =rob@spot.colorado.edu
__status__ =Production

状态通常应为原型,开发 。 __ maintainer __ 应该是谁将修复错误和进行改进,如果导入的人。 __ credits __ __ author __ 不同,因为 __ credits __ 错误修复,建议等,但实际上没有编写代码。


这里您有更多信息,列出 __ author __ __作者__ __ contact __ __ copyright __ __license __ __ deprecated __ __ date __ __ version __ 作为已识别的元数据。


I came across the following header format for Python source files in a document about Python coding guidelines:

#!/usr/bin/env python

"""Foobar.py: Description of what foobar does."""

__author__      = "Barack Obama"
__copyright__   = "Copyright 2009, Planet Earth"

Is this the standard format of headers in the Python world? What other fields/information can I put in the header? Python gurus share your guidelines for good Python source headers :-)

解决方案

Its all metadata for the Foobar module.

The first one is the docstring of the module, that is already explained in Peter's answer.

How do I organize my modules (source files)? (Archive)

The first line of each file shoud be #!/usr/bin/env python. This makes it possible to run the file as a script invoking the interpreter implicitly, e.g. in a CGI context.

Next should be the docstring with a description. If the description is long, the first line should be a short summary that makes sense on its own, separated from the rest by a newline.

All code, including import statements, should follow the docstring. Otherwise, the docstring will not be recognized by the interpreter, and you will not have access to it in interactive sessions (i.e. through obj.__doc__) or when generating documentation with automated tools.

Import built-in modules first, followed by third-party modules, followed by any changes to the path and your own modules. Especially, additions to the path and names of your modules are likely to change rapidly: keeping them in one place makes them easier to find.

Next should be authorship information. This information should follow this format:

__author__ = "Rob Knight, Gavin Huttley, and Peter Maxwell"
__copyright__ = "Copyright 2007, The Cogent Project"
__credits__ = ["Rob Knight", "Peter Maxwell", "Gavin Huttley",
                    "Matthew Wakefield"]
__license__ = "GPL"
__version__ = "1.0.1"
__maintainer__ = "Rob Knight"
__email__ = "rob@spot.colorado.edu"
__status__ = "Production"

Status should typically be one of "Prototype", "Development", or "Production". __maintainer__ should be the person who will fix bugs and make improvements if imported. __credits__ differs from __author__ in that __credits__ includes people who reported bug fixes, made suggestions, etc. but did not actually write the code.

Here you have more information, listing __author__, __authors__, __contact__, __copyright__, __license__, __deprecated__, __date__ and __version__ as recognized metadata.

这篇关于Python文件的常见头格式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆