Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(python) docstring is causing indentation error

def getText(nodelist):
    """Extracts the text between XML tags

    I took this directly from http://docs.python.org/library/xml.dom.minidom.html.
    For example, if I have a tag <Tag>525</Tag> this method returns me '525'
    """
    rc = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc

Gives me IndentationError: unindent does not match any outer indentation level

def getText(nodelist):
    rc = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc

Does not. All I am doing is deleting the docstring comment. What is going on?

like image 932
Tony Stark Avatar asked Feb 11 '10 08:02

Tony Stark


People also ask

How do I fix an indentation error in Python?

Go to the setting of your code editor and enable the option which shows the tab and whitespaces. Once this feature in turned on, you will see small single dots in between your code, where each dot represents whitespace or a tab.

Should docstring be indented?

Handling Docstring IndentationAny indentation in the first line of the docstring (i.e., up to the first newline) is insignificant and removed. Relative indentation of later lines in the docstring is retained. Blank lines should be removed from the beginning and end of the docstring.

Why am I getting an indentation error?

The indentation error can occur when the spaces or tabs are not placed properly. There will not be an issue if the interpreter does not find any issues with the spaces or tabs. If there is an error due to indentation, it will come in between the execution and can be a show stopper.

Why does Python say expected an indented block?

This error occurs when Python is looking for an indented block of code after certain types of statements. The indented block tells Python that the code within the block is relevant to the statement.


2 Answers

Your docstring starts with tabs. Make your code only use spaces for indentation (or only tabs), including the indentation for the docstrings.

like image 166
Alok Singhal Avatar answered Oct 06 '22 20:10

Alok Singhal


Make sure you are not mixing spaces and tabs for your indentation

like image 21
John La Rooy Avatar answered Oct 06 '22 21:10

John La Rooy