This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by SavageSavant
over 9 years

Trouble regarding Multi-line Comments

I’m already past this phase of the course and obviously when I ran the multi-line comments in codecademy it didn’t appear in the interpreter. Yet, when I ran the code in the Python shell on my computer, it read the comments.

Here is the code:

"""This is just a comment, nothing to see here. Python is a very easy language to learn, it is not hard at all. If you're having issues, you better call Saul. 
"""

I am unsure why the Python shell on my laptop is reading the above code, when Python in codecademy doesn’t read it at all and the course mentions the above is the correct syntax for multi-line comments. I am running Python 3.4.1 if that helps. If anyone can assist me I would greatly appreciate it. Thanks in advance.

Answer 5416d13e282ae3321b0037bb

1 vote

Permalink

as far as i know, technically, that is not a comment, it’s a string. python does not actually have multi-line comments but uses docstrings (indicated by triple quotes) for documenting modules classes and functions/methods that can then be accessed with help(NAME) or NAME.__doc__, for example:

def my_function():
    """This function is for demonstration purposes only."""
    pass

print help(my_function)
print my_function.__doc__

strings with triple quotes are just the same as regular ones, triples are used to make them stand out in the code… you can also make multi-line strings with them, not sure if that is acceptable or not though, but i’d use it until someone complains

anyway, the point is, because it’s a string and not a comment, it IS read and evaluated it’s just that nothing is then done with it, it’s like typing then number 5 on its own line

personally i think comments should be done only with # but then again BDFL (creator of python) tweeted this: https://twitter.com/gvanrossum/status/112670605505077248 not quite sure if he means that nothing stays in memory because it’s not saved in a variable anywhere just like if you had a normal string there or that the interpreter actually ignores it

points
Submitted by Jonatan
over 9 years

2 comments

SavageSavant over 9 years

Thanks for the response. So I suppose I should just use the # sign for all comments then? I apologize for any inconvenience, I’m still very new to this language, I’m in the Conditionals & Control Flows (Pyglatin) course. I just want to ensure that I master this language and learn everything I need to learn.

Jonatan over 9 years

is a safe bet, but it’s not a big deal. if you feel the need to use triple quotes, for example to comment out several lines of code and your editor doesn’t support commenting it out for you, or if you for whatever other reason feel like using triple quotes, it’s fine, really. and if someone thinks otherwise they can tell you and changing it is trivial. in cases like this just pick something that suits you, people will disagree either way and if it’s an employer then you’ll just have to be prepared to do it their way

Answer 541624a99c4e9d352700225a

0 votes

Permalink

What indication do you have that the Python shell on your laptop is reading the multi-line comment? I copied and pasted it, saved it in a .py file, and ran it using IDLE with Python 3.2.2, and as expected, it produced no output.

points
Submitted by Glenn Richard
over 9 years

Answer 5416da117c82cabef9003662

0 votes

Permalink

In both Python 2.x and 3.x a triple-quoted string seems to get processed as a string, when entered at the interactive prompt.

Python 3.2.2 (v3.2.2:137e45f15c0b, Sep  3 2011, 17:28:59) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> """This is just a comment, nothing to see here. Python is a very easy language to learn, it is not hard at all. If you're having issues, you better call Saul. 
"""
"This is just a comment, nothing to see here. Python is a very easy language to learn, it is not hard at all. If you're having issues, you better call Saul. \n"
>>>

The interpreter hands it back as a single-quoted string.

But, taken literally, Guido Van Rossum’s tweet suggests that it should generate no code. Perhaps he is referring to a situation where one is used in a .py file that is run in the interpreter. ???

points
Submitted by Glenn Richard
over 9 years

2 comments

Jonatan over 9 years

to be taken with a grain of salt: apparently CPython creates an intermediary version of the code before it is run, so i guess that’s the code that Guido is referring to. and the reason it’s still considered interpreted is that it doesn’t do much in terms of optimization unlike for example java. so when a string in triple quotes is not a docstring or otherwise evaluated it’s not included in that intermediary version

Jonatan over 9 years

so how triple quote strings are treated could vary a lot between implementations, but i would imagine that most of them ignore them when they can. PEP 8 recommends using # for block comments: https://www.python.org/dev/peps/pep-0008/#block-comments