Indentation in function does not work as expected

后端 未结 3 499
盖世英雄少女心
盖世英雄少女心 2020-12-12 01:56

I found something that doesn\'t make sense to me. I am hoping that someone can explain it.

def test(word):
    total = 0
    for l in word:
        print l

         


        
相关标签:
3条回答
  • 2020-12-12 02:48

    I am 100% sure that return total in the second function is on the same level (indented exactly) as print. Probably you use mixed space characters (tab or space).

    0 讨论(0)
  • 2020-12-12 02:51

    The only way you'd get the behaviour you see is if you indented return total to be part of the for loop. If it doesn't look like that in your editor, it's because you have used a TAB character for your return line:

    >>> code_as_posted = '''\
    ... def test(word):
    ...     total = 0
    ...     for l in word:
    ...         print l
    ...     return total
    ... '''
    >>> code_as_posted.splitlines()[-1]
    '\treturn total'
    

    See that \t character escape there? That's a tab character, which expands to up to 8 spaces when interpreted by Python; see Indentation in the Python reference documentation:

    First, tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight (this is intended to be the same rule as used by Unix). The total number of spaces preceding the first non-blank character then determines the line’s indentation.

    As the return exits the loop early, you only ever see the 'p' character printed.

    Both your editor and the Stack Overflow Markdown implementation display tabs as four spaces instead, so it was really hard to spot this specific error here. Run Python with the -tt command line switch to raise exceptions on mixed tab-and-spaces use.

    You need to configure your editor to expand tabs to spaces, or use tabs exclusively. Don't mix the two styles. The Python Style Guide strongly recommends you use spaces only:

    Spaces are the preferred indentation method.

    Tabs should be used solely to remain consistent with code that is already indented with tabs.

    Many editors can also make tabs explicitly visible. In Sublime Text, when you select text, spaces are shown as dimmed dots, tabs as a line:

    Sublime Text editor with code from OP selected; the tab on the return line is visible as a line, spaces are shown as subtle grey dots

    0 讨论(0)
  • 2020-12-12 02:58

    Adding on to others' diagnoses, for a new Python programmer I would suggest the obvious and recommend that they use IDLE to develop their applications. It should be bundled with your interpreter, and it comes out-of-the-box with automatic tab expansion to spaces, as well as other useful Python developing features such as automatic indenting after if/for/def/class etc. statements, multi-line indenting and unindenting (ctrl [/]), and program quick launch with F5. It may be easier to use that than to configure another text editor to recognize Python idioms, and definitely easier to use than notepad or the REPL.

    I would also suggest that when you run scripts from the command line, use the python -tt option, as so:

    C:\file\python -tt myscript.py
    

    This option scans the code for mixing of tabs and spaces, and raises an Exception if it encounters one. Note that this is a mandatory feature in Python 3, so using this option can help you future-proof your code.

    0 讨论(0)
提交回复
热议问题