最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果

Python crifan 23006浏览

先给出几个常见的错误和解决办法:

Python中常见的和代码缩进有关的问题

IndentationError: unexpected indent

举例:

这样的代码:

1
2
3
4
5
6
7
# -*- coding: utf-8 -*-
import pickle
import math
    ipath = "D:/123"
    fileobj= open(ipath, 'rb')
    pdata= pickle.load(fileobj)
    fileobj.close()

运行结果是:

D:\tmp\tmp_dev_root\python\pick_dump_error>pickle_dump.py

  File "D:\tmp\tmp_dev_root\python\pick_dump_error\pickle_dump.py", line 4

    ipath = "D:/123"

    ^

IndentationError: unexpected indent

原因:

Python解析器,发现你的代码缩进有问题。

此处的问题是,在

import math

之后,突然来了个:

1
ipath = "D:/123"

而此种缩进,前面即不是函数定义:

1
2
3
def someFunction():
    xxx
    xxx

也不是其他的形式,所以,语法上,就不支持,

即Python解析器,不知道这段代码,是属于哪个范围的,无法解析这样的代码。

 

代码执行无结果

比如,这样的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【教程】详解Python中代码缩进(Indent)
<blockquote class="wp-embedded-content" data-secret="sA23A0F7Hy"><a href="https://www.crifan.com/tutorial_python_indent/" data-original-title="" title="">【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;" title="《 【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果 》—在路上" src="https://www.crifan.com/tutorial_python_indent/embed/#?secret=5td5fK6Xxl#?secret=sA23A0F7Hy" data-secret="sA23A0F7Hy" width="500" height="282" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
 
Author:     Crifan Li
Version:    2012-12-24
Contact:    admin at crifan dot com
"""
 
def indentDemo():
    print "Just demo for python code indent";
     
#if __name__ == "__main__":
    indentDemo();

执行出来的结果是空的:

D:\tmp\tmp_dev_root\python\python_indent>python_indent.py

D:\tmp\tmp_dev_root\python\python_indent>

即,啥都没输出,这和我们要的结果,明显不符。

其原因在于:

上述代码中的:

1
indentDemo();

是属于函数indentDemo中的代码,而不是此脚本所能执行出来的代码。

而想要达到我们要的效果,即能够执行到对应的函数indentDemo,可以改为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【教程】详解Python中代码缩进(Indent)
<blockquote class="wp-embedded-content" data-secret="sA23A0F7Hy"><a href="https://www.crifan.com/tutorial_python_indent/" data-original-title="" title="">【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;" title="《 【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果 》—在路上" src="https://www.crifan.com/tutorial_python_indent/embed/#?secret=5td5fK6Xxl#?secret=sA23A0F7Hy" data-secret="sA23A0F7Hy" width="500" height="282" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
 
Author:     Crifan Li
Version:    2012-12-24
Contact:    admin at crifan dot com
"""
 
def indentDemo():
    print "Just demo for python code indent";
     
if __name__ == "__main__":
    indentDemo();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【教程】详解Python中代码缩进(Indent)
<blockquote class="wp-embedded-content" data-secret="sA23A0F7Hy"><a href="https://www.crifan.com/tutorial_python_indent/" data-original-title="" title="">【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;" title="《 【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果 》—在路上" src="https://www.crifan.com/tutorial_python_indent/embed/#?secret=5td5fK6Xxl#?secret=sA23A0F7Hy" data-secret="sA23A0F7Hy" width="500" height="282" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
 
Author:     Crifan Li
Version:    2012-12-24
Contact:    admin at crifan dot com
"""
 
def indentDemo():
    print "Just demo for python code indent";
     
#if __name__ == "__main__":
indentDemo();

输出的结果都是:

D:\tmp\tmp_dev_root\python\python_indent>python_indent.py

Just demo for python code indent

D:\tmp\tmp_dev_root\python\python_indent>python_indent.py


再来解释具体的含义:

Python中的代码缩进

Python中,是通过代码的缩进,来决定代码的逻辑的。

通俗的说,Python中的代码的缩进,不是为了好看,而是觉得代码的含义,上下行代码之间的关系。

缩进弄错了,就会导致程序出错,执行结果变成不是你想要的了。

关于第一行要执行的代码

你写了Python代码,如果运行后没有输出你想要的结果,那么很可能是由于你的缩进所导致的。

就拿上面的错误例子“代码执行无结果”来说,实际上,Python解释器,去执行你的代码的逻辑是:

can run first line code

 

find no more code so quit

 

check __name__ is __main__ so exec code

注:

关于__name__和__main__的知识,不了解的去看:

【整理】Python中的__name__和__main__含义详解

 

代码块

对应的,上述几个图解中,def indentDemo后面的代码,也就被因此成为代码块:

code block eg

说白了,就是一个逻辑上的概念,可以简单理解为其他语言中的,一个函数内的代码,一个if判断内的代码等等相应的概念;

 

其他语言中的代码缩进:只是决定了是否好看,不影响代码逻辑和运行结果

可见,Python中的代码缩进,觉得了,不同行代码之间的,代码的逻辑关系。

而与此相对应的,其他的多数的语言,比如C,C#,Java等等,都是通过对应的大括号之类的符号,来决定的代码的逻辑关系的。

把上述Python代码,写出类似于的C等语言的代码,就可以写成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
Function:
【教程】详解Python中代码缩进(Indent)
<blockquote class="wp-embedded-content" data-secret="sA23A0F7Hy"><a href="https://www.crifan.com/tutorial_python_indent/" data-original-title="" title="">【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;" title="《 【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果 》—在路上" src="https://www.crifan.com/tutorial_python_indent/embed/#?secret=5td5fK6Xxl#?secret=sA23A0F7Hy" data-secret="sA23A0F7Hy" width="500" height="282" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
 
demo indent in C code
 
Author:     Crifan Li
Version:    2012-12-24
Contact:    admin at crifan dot com
*/
 
#include <stdio.h>
 
void indentDemo(void){
    printf("Just demo for c code indent");
};
 
int main(void){
    indentDemo();
     
    return 0;
}

对应的,在Cygwin中编译后输出为:

CLi@PC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/python/python_indent

$ gcc c_indent.c -o c_indent.exe

CLi@PC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/python/python_indent

$ ./c_indent

Just demo for c code indent

上述的代码,很明显,是加了对应的缩进,但是C等语言中的缩进,说白了,只是为了代码看起来更“好看”,使得“看起来”代码的逻辑更清晰。

实际上,你要是,本来就是,有个不好的编程习惯,或者此处故意地,没有合理的缩进,写出这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
Function:
【教程】详解Python中代码缩进(Indent)
<blockquote class="wp-embedded-content" data-secret="sA23A0F7Hy"><a href="https://www.crifan.com/tutorial_python_indent/" data-original-title="" title="">【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;" title="《 【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果 》—在路上" src="https://www.crifan.com/tutorial_python_indent/embed/#?secret=5td5fK6Xxl#?secret=sA23A0F7Hy" data-secret="sA23A0F7Hy" width="500" height="282" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
 
demo indent in C code
 
Author:     Crifan Li
Version:    2012-12-24
Contact:    admin at crifan dot com
*/
 
#include <stdio.h>
 
void indentDemo(void){
printf("Just demo for c code indent");
};
 
int main(void){
indentDemo();
 
return 0;
}

实际上的代码的执行效果,和代码本身的含义,都是没变化的,只是“看起来”,很不好而已,因为没了合理的,必要的缩进,代码逻辑关系看起来就很不清晰。

当然,甚至,此处可以故意改为这种:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
Function:
【教程】详解Python中代码缩进(Indent)
<blockquote class="wp-embedded-content" data-secret="sA23A0F7Hy"><a href="https://www.crifan.com/tutorial_python_indent/" data-original-title="" title="">【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;" title="《 【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果 》—在路上" src="https://www.crifan.com/tutorial_python_indent/embed/#?secret=5td5fK6Xxl#?secret=sA23A0F7Hy" data-secret="sA23A0F7Hy" width="500" height="282" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
 
demo indent in C code
 
Author:     Crifan Li
Version:    2012-12-24
Contact:    admin at crifan dot com
*/
 
#include <stdio.h>
 
void indentDemo(void){printf("Just demo for c code indent");};
 
int main(void){indentDemo();return 0;}

此时,代码的含义本身,仍然是没有变化的,只是,同上,更加不好,代码看起来更加的乱而已。

 

总结:Python中的代码缩进决定了代码的内在逻辑和执行结果

Python代码缩进,决定了代码的内在逻辑关系,决定了代码的执行结果的对错;

相对而言的其他语言,比如C/C#/Java/…,代码缩进,只是为了代码“看起来”的逻辑更加清晰,但是不影响代码的内在逻辑关系,不影响代码执行结果的。

转载请注明:在路上 » 【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果

94 queries in 0.347 seconds, using 19.20MB memory