Origin of using # as a comment in Python?

So I'm just like that mental blast dude! I looked at my Python source code and read some comments, and then looked at the comments again. When I came across this:

#!/usr/bin/env python # A regular comment 

What made me think, # selected as the symbol for starting the comment, because it would allow the python program to run in the shell, for example:

./test.py

and then ignore as soon as the python interpreter is started?

+4
source share
4 answers

Yes.

Using # to start a comment is an agreement followed by all the major interpreted languages ​​designed to work with POSIX systems (i.e. not Windows).

It also agrees well with the sequence "#!" at the beginning of the file, the OS recognizes β€œrun the command on this line” when trying to run the script file itself.

But basically it is a generally accepted convention. If python did not use # to start a comment, this is confusing for a lot of people.

EDIT

Using "#" as a comment marker seems to precede "#!" hash designation . "#!" was introduced by Dennis Ritchie between Unix 7 and 8, and languages ​​that support # as a comment marker existed before. For example, the Bourne shell was already installed by default when version 7 of Unix was introduced.

Consequently, the agreement to use "#" as a comment marker probably affected the choice of "#!" as a command line marker, not the other way around.

+13
source

Using # for comments happened before Python appeared. The shebang convention ( #!/usr/bin/env python ) is almost as old as UNIX itself. These two are intertwined for many interpreted (aka-shells) languages.

You can also study the history of the history of shebang !

+6
source

"The rest of the line after the X character" is by far the most convenient way to make comments if you have X (C ++ should have used two characters, // , for this purpose, to offer an alternative to C clumsy PL / I-inspired '/'... '/' "parentheses" ).

Almost all Ascii printable characters can be used for other purposes in Python - if the choice is between # and ? , and the first of them is already known from its use in sh, bash, tcl, perl, awk, ... - this is not a very difficult choice, is it? The convenience of a hashbang is just a video.

+1
source

Very good, you're right.

This is known as shebang or hash-bang. I assume that the scripting language can use any comment he likes and allow #! in the first line as a comment, but the easiest way is to simply # make the comment character ...

0
source

Source: https://habr.com/ru/post/1301659/


All Articles