This forum is now read-only. Please use our new forums! Go to forums
Multiline comments don't actually exist in Python
As part of the Python course it is taught that in order to do a multiline comment one should use """triple quotes"""
. This is wrong. Python only has one way of doing comments and that is using #
.
Triple quotes are treated as regular strings with the exception that they can span multiple lines. By regular strings I mean that if they are not assigned to a variable they will be immediately garbage collected as soon as that code executes. hence are not ignored by the interpreter in the same way that #a comment
is.
The only exception to the garbage collection fact is when they are placed immediately after a function or class definition or on top of a module, in which case they are called docstrings and made available via the special variable myobj.__doc__
.
Due to the existance of docstrings I’ve seen a lot of people confused about the use of """
elsewhere.
I suggest that the multiline comment lesson is removed or replaced with the instructions from the official PEP8 - Block comments section.
Answer 5397bb468c1ccc0d76000792
Disappointingly the course has NOT been fixed and STILL, ONE YEAR LATER from the original comment above, teaches incorrect commenting. I also noted a module where the lesson showed comments using slashes “//“. Strange AND misleading!
They really need to update their backend testing system to use the latest builds so that NEW LEARNERS, who are likely using the latest build, will get consistent results and correct instruction.
So, although I am glad this is free to end-users, the quality should be improved - definitely after a YEAR.
Answer 5594c69793767623c5000431
Then Guido must be confused as well, though he did write the language….
Guido van Rossum @gvanrossum 10 Sep 2011 @BSUCSClub Python tip: You can use multi-line strings as multi-line comments. Unless used as docstrings, they generate no code! :-)
Answer 506070f30aef700002074bbe
He understood that “””triple quotes””” were used to include documentation within the code Ejm:
def x_intercept (m, b):
"" "
Return the x intercept of the line y = m * x + b. The x intercept of a
line is the point at Which it crosses the x axis (y = 0).
"" "
return-b / m
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
5 comments
I learned that triple quotes can be used for documentation elsewhere. I was referring to http://www.codecademy.com/courses/introduction-to-python-6WeG3/2#!/exercises/1 which points out that triple quotes can be used for block comenting.
You are absolutely correct Unode. The lesson inappropriately refers to them as multi-line comments when they are in face NOT multi-line comments and do affect memory allocation in the interpreter.
this lesson is still in the python track. triple-quotes are definitely not comments. the interpreter will read them and you can assign them to a variable. that doesn’t sound like comment behavior.
I also noticed this and was confused. As mentioned by those before me, This lesson should be removed or worked into a separate lesson for docstrings.
okay, so I progressed to the next section and now I’m very confused.
when I type my comment with triple quotes in this comment lesson, it will print it on the console which is different from using the hash.
I got to the “subtraction” lesson and in that lesson the triple quotes are used to comment on the code. This comment does not print in the console so it seems that the triple quotes do work.
I like to poke around and test things out, so coded the same thing in both lessons and the output on the console was the same. There was a difference when I deleted everything except the comment though; it printed on the console in the “comment” lesson and did not print in the console in the “subtraction” lesson. Very odd; something is buggy or I don’t understand something.