Python:什么是标题?

Vic*_*kie 11 python header

我是Python和编程的新手.我正在大学学习一个模块,这需要我用Python编写一些相当基础的程序.但是,我在上次作业时得到了这个反馈:

应该有一个标题块,包含文件名,作者姓名,创建日期,修改日期和python版本

什么是标题块?它只是代码顶部的注释,还是在程序运行时打印的内容?或者是其他东西?

Vyk*_*tor 19

python中有一个名为Docstring的东西(这里有一些关于如何编写一般的python代码的约定 - PEP 8),它们被三重单引号'''或三重双引号转义,"""非常适合多行注释:

'''
    File name: test.py
    Author: Peter Test
    Date created: 4/20/2013
    Date last modified: 4/25/2013
    Python Version: 2.7
'''
Run Code Online (Sandbox Code Playgroud)

您也可以稍后(在编写模块时)使用特殊变量来专门包含信息:

__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"
Run Code Online (Sandbox Code Playgroud)

更多细节在这里回答.


Jay*_*zzo 5

我的看法

我使用这种格式,因为我正在学习,“这更多是为了我自己的理智,而不是必需品。”

因为我喜欢一致性。所以,我像这样开始我的文件。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =============================================================================
# Created By  : Jeromie Kirchoff
# Created Date: Mon August 18 18:54:00 PDT 2018
# =============================================================================
"""The Module Has Been Build for..."""
# =============================================================================
# Imports
# =============================================================================
from ... import ...
<more code...>
Run Code Online (Sandbox Code Playgroud)
  1. 第一行是社邦
    1. 我知道There's no reason for most Python files to have a shebang line但是,对我来说,我觉得它让用户知道我明确地为 python3 编写了这个。和我的 Mac 一样,我有 python2 和 python3。
  2. 第 2 行是编码,再次只是为了澄清
    1. 正如我们中的一些人忘记了当我们处理多个来源(API、数据库、电子邮件等)
  3. 第 3 行更多是我自己对最大 80 个字符的直观表示。
    1. 我知道“哦,天哪,为什么?!?” 再次,这使我可以将代码保持在 80 个字符以内,以实现可视化表示和可读性。
  4. 第 4 行和第 5 行只是我自己的跟踪方式,因为当在一个大团队中工作时,将谁写在手边是有帮助的,并且可以节省一些时间来查看您的GitHub. 再次无关紧要,只是我为了理智而捡到的东西。
  5. 第 7 行是您的 Docstring,它位于每个Flake8的每个 python 文件的顶部。

同样,这只是我的偏好。在 a 中,working environment您必须赢得所有人的支持才能改变事实上的行为。我可以继续谈论这个,但我们都知道,至少在工作场所。

标题块

  • 什么是标题块?
  • 它只是代码顶部的注释还是程序运行时打印的内容?
  • 或者是其他东西?

因此,在这种大学环境中:

标题块或注释

标题注释出现在文件的顶部。这些行通常包括文件名、作者、日期、版本号以及文件用途和包含的内容的描述。对于课堂作业,标题还应包括课程名称、编号、部分、讲师和作业编号等内容。

  • 它只是代码顶部的注释还是程序运行时打印的内容?或者是其他东西?

好吧,您的教授可以对此进行不同的解释,展示并询问!

“如果你从不问,答案总是不。”

IE:

# Course: CS108
# Laboratory: A13
# Date: 2018/08/18
# Username: JayRizzo
# Name: Jeromie Kirchoff
# Description: My First Project Program.
Run Code Online (Sandbox Code Playgroud)

如果您正在寻找 Overkill:

或使用“模块级 Dunder 名称”的 python 方式

标准模块级 Dunder 名称

__author__ = 'Jeromie Kirchoff'
__copyright__ = 'Copyright 2018, Your Project'
__credits__ = ['Jeromie Kirchoff', 'Victoria Mackie']
__license__ = 'MSU'  # Makin' Shi* Up!
__version__ = '1.0.1'
__maintainer__ = 'Jeromie Kirchoff'
__email__ = 'Jahomie04@gmail.com'
__status__ = 'Prototype'
Run Code Online (Sandbox Code Playgroud)

添加您自己的自定义名称:

__course__ = 'cs108'
__teammates__ = ['Jeromie Kirchoff']
__laboratory__ = 'A13'
__date__ = '2018/08/18'
__username__ = 'JayRizzo'
__description__ = 'My First Project Program.'
Run Code Online (Sandbox Code Playgroud)

然后,如果教师愿意,只需添加一些代码即可打印。

print('# ' + '=' * 78)
print('Author: ' + __author__)
print('Teammates: ' + ', '.join(__teammates__))
print('Copyright: ' + __copyright__)
print('Credits: ' + ', '.join(__credits__))
print('License: ' + __license__)
print('Version: ' + __version__)
print('Maintainer: ' + __maintainer__)
print('Email: ' + __email__)
print('Status: ' + __status__)
print('Course: ' + __course__)
print('Laboratory: ' + __laboratory__)
print('Date: ' + __date__)
print('Username: ' + __username__)
print('Description: ' + __description__)
print('# ' + '=' * 78)
Run Code Online (Sandbox Code Playgroud)

最终结果

每次程序被调用时,它都会显示列表。

$ python3 custom_header.py
# ==============================================================================
Author: Jeromie Kirchoff
Teammates: Jeromie Kirchoff
Copyright: Copyright 2018, Your Project
Credits: Jeromie Kirchoff, Victoria Mackie
License: MSU
Version: 1.0.1
Maintainer: Jeromie Kirchoff
Email: Jahomie04@gmail.com
Status: Prototype
Course: CS108
Laboratory: A13
Date: 2018/08/18
Username: JayRizzo
Description: My First Project Program.
# ==============================================================================
Run Code Online (Sandbox Code Playgroud)

注意:如果你扩展你的程序,只需在init .py 中设置一次,你应该都设置好了,但再次与教授核对。

如果想要脚本签出我的github。


rec*_*ive 0

在这种情况下,你是正确的。标头块是指位于源文件顶部的一组注释,其中包含所请求的信息。它不需要包含任何执行任何操作的代码。