简体   繁体   English

在 Python 中使用“#”作为注释而 C 使用“#”作为预处理器指令背后的想法/概念是什么?

[英]What is the idea/notion behind using '#' as comments in Python while C uses '#' for pre-processor directive?

My guess:我猜:

In Python:在 Python 中:

// was used for floor division and they couldn't come up with any other alternative symbol for floor division so they couldn't use // for comments in Python. //用于地板除法,他们无法为地板除法提供任何其他替代符号,因此他们无法在 Python 中使用//进行注释。

# was an available character to be used in Python as there is no concept of pre-processing, and they made a choice to use # for comments. #是 Python 中可用的字符,因为没有预处理的概念,他们选择使用#来表示注释。

I'm afraid your assumption is false: the floor division operator // is quite recent in Python, whereas # comments are part of the original design.恐怕您的假设是错误的:地板除法运算符//在 Python 中是相当新的,而#注释是原始设计的一部分。

The origin of # comments is older: #评论的起源较早:

  • the early unix shells (the original sh command started in 1971, csh from 1978 and the Bourne shell 1979) introduced the use of # to start line comments, followed by all later unix shells.早期的 unix shell(最初的sh命令始于 1971 年,csh 始于 1978 年,Bourne shell 始于 1979 年)引入了使用#来开始行注释,随后是所有后来的 unix shell。
  • Many script like programming languages already used # for comments: sed (1974), make (1976), awk (1977) ...许多脚本之类的编程语言已经使用#作为注释:sed (1974), make (1976), awk (1977) ...
  • # was also used for comments in configuration files #也用于配置文件中的注释
  • later scripting languages followed the same convention: TCL (1988), Perl (1988), Python (1991), PHP (1994), Ruby (1995) and more recently Cobra, Seed7, Windows PowerShell, R, Maple, Elixir, Julia, Nim...后来的脚本语言遵循相同的约定:TCL (1988)、Perl (1988)、Python (1991)、PHP (1994)、Ruby (1995) 以及最近的 Cobra、Seed7、Windows PowerShell、R、Maple、Elixir、Julia、尼姆...

Regarding the C preprocessor, here is a quote from Dennis M. Ritchie's own memories of The Development of the C Language about the origin of the C preprocessor and the true meaning of the # character:关于 C 预处理器,这里引用丹尼斯#里奇 (Dennis M. Ritchie) 自己的《 The Development of the C Language

Many other changes occurred around 1972-3, but the most important was the introduction of the preprocessor, partly at the urging of Alan Snyder [Snyder 74] , but also in recognition of the utility of the the file-inclusion mechanisms available in BCPL and PL/I. 1972-3 年左右发生了许多其他变化,但最重要的是引入了预处理器,部分是在 Alan Snyder [Snyder 74]的敦促下,但也认识到 BCPL 中可用的文件包含机制的效用和PL/I。 Its original version was exceedingly simple, and provided only included files and simple string replacements: #include and #define of parameterless macros.它的原始版本非常简单,只提供包含文件和简单的字符串替换: #include#define无参数宏。 Soon thereafter, it was extended, mostly by Mike Lesk and then by John Reiser, to incorporate macros with arguments and conditional compilation.此后不久,主要由 Mike Lesk 和 John Reiser 对其进行了扩展,以将宏与参数和条件编译结合起来。 The preprocessor was originally considered an optional adjunct to the language itself.预处理器最初被认为是语言本身的可选附件。 Indeed, for some years, it was not even invoked unless the source program contained a special signal at its beginning.事实上,多年来,除非源程序在开始时包含特殊信号,否则它甚至不会被调用。 This attitude persisted, and explains both the incomplete integration of the syntax of the preprocessor with the rest of the language and the imprecision of its description in early reference manuals.这种态度持续存在,并解释了预处理器语法与语言其余部分的不完全集成以及早期参考手册中对其描述的不精确性。

# was used as a special character at the beginning of a C source file to determine if the preprocessor was to be invoked. #用作 C 源文件开头的特殊字符,以确定是否要调用预处理器。

PL/I file include directives use %INCLUDE and BCPL uses GET "libhdr" PL/I 文件包含指令使用%INCLUDE ,BCPL 使用GET "libhdr"

C was by no means the only prior art available when Guido was choosing the details of Python language syntax.当 Guido 选择 Python 语言语法的细节时,C 绝不是唯一可用的现有技术。 # is in fact a pretty common comment-introduction character, especially for scripting languages. #实际上是一个非常常见的注释介绍字符,尤其是对于脚本语言。 Examples include the Bourne family of shells, the Csh family of shells, Perl, awk, and sed, all of which predate Python.示例包括 Bourne shell 系列、Csh shell 系列、Perl、awk 和 sed,所有这些都早于 Python。 I have always supposed that this aspect of Python's syntax was most influenced by this fairly large group of languages.我一直认为 Python 语法的这一方面受这一相当大的语言群体的影响最大。

Whatever the influences were, they did not include consideration of a conflict with the use of // for floor division, as that operator was not introduced until much later.无论影响是什么,它们都没有考虑与使用//进行楼层划分的冲突,因为直到很久以后才引入该运算符。

The use of // comments dates back to 1967 [or earlier]. //注释的使用可以追溯到 1967 [或更早]。

I came across Keith Thompson's answer: With arrays, why is it the case that a[5] == 5[a]?我遇到了 Keith Thompson 的回答: 对于数组,为什么 a[5] == 5[a]?

In it are links to language reference manuals for B and BCPL at Bell Labs:其中是贝尔实验室BBCPL语言参考手册的链接:

  1. The B language manual from 1972 (precursor to C ): User's Reference to B 1972 年的B语言手册( C的前身):用户对 B 的参考
  2. The BCPL language manual from 1967 (precursor to B ): Martin Richards's BCPL Reference Manual, 1967 1967 年的BCPL语言手册( B的前身): Martin Richards 的 BCPL 参考手册,1967
  3. From a link there, we have a link to a PDF file that is a transcription of MIT Project MAC Memorandum-M-352从那里的链接,我们有一个 PDF 文件的链接,该文件是MIT 项目 MAC 备忘录-M-352的转录

From that memorandum (the BCPL manual), in section 2.1.2 (b):从该备忘录( BCPL手册)第 2.1.2 (b) 节中:

2.1.2 Hardware Conventions and Preprocessor Rules 2.1.2 硬件约定和预处理器规则

(a) If the implementation character set contains both capital and small letters then the following conventions hold: (a) 如果实现字符集同时包含大写字母和小写字母,则以下约定成立:

(1) A name is either a single small letter or a sequence of letters and digits starting with a capital letter. (1) 名称可以是一个小写字母,也可以是一个以大写字母开头的字母和数字序列。 The character immediately following a name may not be a letter or a digit.紧跟在名称后面的字符不能是字母或数字。

(2) A sequence of two or more small letters which is not part of a NAME, SECTBRA, SECTKET or STRINGCONST is a reserved system word and may be used to represent a canonical symbol. (2) 不属于 NAME、SECTBRA、SECTKET 或 STRINGCONST 的两个或多个小字母的序列是保留的系统字,可用于表示规范符号。
For example: let and logor could be used to represent LET and LOGOR but Let and Logor are names.例如:let 和 logor 可以用来表示 LET 和 LOGOR,但 Let 和 Logor 是名称。

(b) User's comment may be included in a program between a double slash '//' and the end of the line. (b) 用户的注释可以包含在双斜杠“//”和行尾之间的程序中。 Example:例子:

let R[] be // This routine refills the vector Symb let R[] be // 这个例程重新填充向量 Symb
§ for i = 1 to 200 do Readch [INPUT, lv Symb*[i]] § § for i = 1 to 200 do Readch [INPUT, lv Symb*[i]] §

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM