комментарии в строках с тройными кавычками сбой простой программы на Python - PullRequest
0 голосов
/ 19 марта 2019

Я обнаружил эту странную проблему, когда пытался добавить комментарии к своему коду.Я использовал строки с тройными кавычками для комментирования, но программа потерпела крах, выдав следующую ошибку:

IndentationError: unexpected indent

Когда я использую # для комментирования строк с тройными кавычками, все работаетобычно.Кто-нибудь знает причину этой ошибки и как я мог это исправить?

Мой код:

#This programs show that comments using # rather than """ """

def main():
    print("let's do something")
#Try using hashtag to comment this block to get code working
'''
    Note following block gives you a non-sense indent error
    The next step would be to consider how to get all the words from spam and ham
    folder from different directory. My suggestion would be do it twice and then
    concentrate two lists

    Frist think about the most efficient way
    For example, we might need to get rid off the duplicated words in the beginning

    The thoughts of writing the algorithem to create the dictionary

    Method-1:
    1. To append all the list from the email all-together
    2. Eliminate those duplicated words

    cons: the list might become super large

    I Choose method-2 to save the memory
    Method-2:
    1. kill the duplicated words in each string
    2. Only append elements that is not already in the dictionary

    Note:
    1. In this case, the length of feature actually was determined by the
    training cohorts, as we used the different English terms to decide feature

    cons: the process time might be super long
'''
    def wtf_python(var1, var2):
        var3 = var1 + var2 + (var1*var2)
        return var3

    wtfRst1 = wtf_python(1,2)
    wtfRst2 = wtf_python(3,4)

    rstAll = { "wtfRst1" : wtfRst1,
               "wtfRst2" : wtfRst2
    }
    return(rstAll)

if __name__ == "__main__":
    mainRst = main()
    print("wtfRst1 is :\n", mainRst['wtfRst1'])
    print("wtfRst2 is :\n", mainRst['wtfRst2'])

Ответы [ 3 ]

1 голос
/ 19 марта 2019

Виновник :

Перемещение комментариев внутри определения функции:

Причина :

Поскольку строки с тройными кавычками являются допустимыми выражениями python exp, они должны обрабатываться аналогично, то есть внутри области действия функции.

Следовательно

def main():
    print("let's do something")
    #Try using hashtag to comment this block to get code working
    '''
        Note following block gives you a non-sense indent error
        The next step would be to consider how to get all the words from spam and ham
        folder from different directory. My suggestion would be do it twice and then
        concentrate two lists

        Frist think about the most efficient way
        For example, we might need to get rid off the duplicated words in the beginning

        The thoughts of writing the algorithem to create the dictionary

        Method-1:
        1. To append all the list from the email all-together
        2. Eliminate those duplicated words

        cons: the list might become super large

        I Choose method-2 to save the memory
        Method-2:
        1. kill the duplicated words in each string
        2. Only append elements that is not already in the dictionary

        Note:
        1. In this case, the length of feature actually was determined by the
        training cohorts, as we used the different English terms to decide feature

        cons: the process time might be super long
    '''
    def wtf_python(var1, var2):
        var3 = var1 + var2 + (var1*var2)
        return var3

    wtfRst1 = wtf_python(1,2)
    wtfRst2 = wtf_python(3,4)

    rstAll = { "wtfRst1" : wtfRst1,
               "wtfRst2" : wtfRst2
    }
    return(rstAll)

if __name__ == "__main__":
    mainRst = main()
    print("wtfRst1 is :\n", mainRst['wtfRst1'])
    print("wtfRst2 is :\n", mainRst['wtfRst2'])

OUTPUT

let's do something
wtfRst1 is :
 5
wtfRst2 is :
 19
1 голос
/ 19 марта 2019

Строки в тройных кавычках в качестве комментариев должны быть действительными строками Python. Допустимые строки Python должны иметь правильный отступ.

Python видит многострочную строку, оценивает ее, но, поскольку вы не назначаете ей переменную, строка отбрасывается в следующей строке.

1 голос
/ 19 марта 2019

Вы должны сдвинуть уровень отступа строк тройных кавычек на один тег вправо.

Хотя строки тройных кавычек часто используются в качестве комментариев, они являются обычными выражениями python, поэтому они должны следовать языкусинтаксис.

...