Home > Back-end >  What is the idea/notion behind using '#' as comments in Python while C uses '#'
What is the idea/notion behind using '#' as comments in Python while C uses '#'

Time:07-04

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.

CodePudding user response:

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"

CodePudding user response:

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.

  • Related