我是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)
更多细节在这里回答.
我使用这种格式,因为我正在学习,“这更多是为了我自己的理智,而不是必需品。”
因为我喜欢一致性。所以,我像这样开始我的文件。
#!/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)
There's no reason for most Python files to have a shebang line但是,对我来说,我觉得它让用户知道我明确地为 python3 编写了这个。和我的 Mac 一样,我有 python2 和 python3。GitHub. 再次无关紧要,只是我为了理智而捡到的东西。同样,这只是我的偏好。在 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)
或使用“模块级 Dunder 名称”的 python 方式
__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 中设置一次,你应该都设置好了,但再次与教授核对。