我不明白为什么Python给出"预期的凹痕块"错误?
""" This module prints all the items within a list"""
def print_lol(the_list):
""" The following for loop iterates over every item in the list and checks whether
the list item is another list or not. in case the list item is another list it recalls the function else it prints the ist item"""
for each_item in the_list:
if isinstance(each_item, list):
print_lol(each_item)
else:
print(each_item)
答案
您必须在函数定义之后缩进DOCSTRING(第3、4行):
def print_lol(the_list):
"""this doesn't works"""
print 'Ain't happening'
缩进:
def print_lol(the_list):
"""this works!"""
print 'Aaaand it's happening'
或者您可以使用#
评论:
def print_lol(the_list):
#this works, too!
print 'Hohoho'
另外,您可以看到PEP 257关于docstrings。
希望这可以帮助!