Reputation: 1863
My code doesn't run if I use triple quotes before an else statement:
def do_something(test_option=False):
"""
function to do something
:param test_option: bool
:return: None
"""
'''
Testing
'''
if test_option==True:
print("testing")
'''
Visualization
'''
else:
# do thing
I get a red squiggly under my else
.
Why is this?
I understand that ''' can also be used for function annotation. Is it not supposed to be used anywhere else?
Suggestions for alternative ways to highlight organization of code? (My IDE literally highlights ''' in yellow which I have been using organize different sections of my code.)
Upvotes: 3
Views: 1868
Reputation: 106648
Triple quotes represent a string literal, not a comment, so by placing the Visualization
outside the if
block with the same level of indentation you are effectively ending the if
statement, so the following else
clause becomes invalid. Indent Visualization
inside the if
block to avoid such an error.
Upvotes: 6
Reputation: 33950
Only use triple-quotes for docstrings (or else other multiline strings, which are rare).
Don't try to use them for comments. Use # your comment goes here...
def do_something(test_option=False):
"""
function to do something
:param test_option: bool
:return: None
"""
# Testing
if test_option==True:
print("testing")
# Visualization
else:
# do thing
(Note by the way that comments are more compact)
Upvotes: 2
Reputation: 2331
The issue is that Python thinks that you've ended the if statement. You went back an indent to put your comment in, so it has invalid syntax. Try this:
def do_something(test_option=False):
"""
function to do something
:param test_option: bool
:return: None
"""
'''
Testing
'''
if test_option==True:
print("testing")
'''
Visualization
'''
else:
# do thing
pass
Upvotes: 1