shar_m
shar_m

Reputation: 39

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

My guess:

In 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.

# 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.

Upvotes: 1

Views: 122

Answers (3)

Craig Estey
Craig Estey

Reputation: 33601

The use of // comments dates back to 1967 [or earlier].

I came across Keith Thompson's answer: With arrays, why is it the case that a[5] == 5[a]?

In it are links to language reference manuals for B and BCPL at Bell Labs:

  1. The B language manual from 1972 (precursor to C): User's Reference to B
  2. The BCPL language manual from 1967 (precursor to B): Martin Richards's BCPL Reference Manual, 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

From that memorandum (the BCPL manual), in section 2.1.2 (b):

2.1.2 Hardware Conventions and Preprocessor Rules

(a) If the implementation character set contains both capital and small letters then the following conventions hold:

(1) A name is either a single small letter or a sequence of letters and digits starting with a capital letter. 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.
For example: let and logor could be used to represent LET and LOGOR but Let and Logor are names.

(b) User’s comment may be included in a program between a double slash '//' and the end of the line. Example:

let R[] be // This routine refills the vector Symb
§ for i = 1 to 200 do Readch [INPUT, lv Symb*[i]] §

Upvotes: 1

chqrlie
chqrlie

Reputation: 144770

I'm afraid your assumption is false: the floor division operator // is quite recent in Python, whereas # comments are part of the original design.

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.
  • Many script like programming languages already used # for comments: 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...

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:

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. Its original version was exceedingly simple, and provided only included files and simple string replacements: #include and #define of parameterless macros. Soon thereafter, it was extended, mostly by Mike Lesk and then by John Reiser, to incorporate macros with arguments and conditional compilation. 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.

PL/I file include directives use %INCLUDE and BCPL uses GET "libhdr"

Upvotes: 4

John Bollinger
John Bollinger

Reputation: 180266

C was by no means the only prior art available when Guido was choosing the details of Python language syntax. # 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. I have always supposed that this aspect of Python's syntax was most influenced by this fairly large group of languages.

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.

Upvotes: 2

Related Questions