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

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

Python crifan 21849浏览 0评论

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

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

IndentationError: unexpected indent

举例:

这样的代码:

# -*- 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

之后,突然来了个:

    ipath = "D:/123"

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

def someFunction():
    xxx
    xxx

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

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

 

代码执行无结果

比如,这样的代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【教程】详解Python中代码缩进(Indent)
https://www.crifan.com/tutorial_python_indent

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>

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

其原因在于:

上述代码中的:

    indentDemo();

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

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

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【教程】详解Python中代码缩进(Indent)
https://www.crifan.com/tutorial_python_indent

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();

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【教程】详解Python中代码缩进(Indent)
https://www.crifan.com/tutorial_python_indent

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等语言的代码,就可以写成:

/*
Function:
【教程】详解Python中代码缩进(Indent)
https://www.crifan.com/tutorial_python_indent

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等语言中的缩进,说白了,只是为了代码看起来更“好看”,使得“看起来”代码的逻辑更清晰。

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

/*
Function:
【教程】详解Python中代码缩进(Indent)
https://www.crifan.com/tutorial_python_indent

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;
}

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

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

/*
Function:
【教程】详解Python中代码缩进(Indent)
https://www.crifan.com/tutorial_python_indent

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):影响代码的内在逻辑关系和执行结果

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (4)

  1. 但在比如在写函数参数、或者一维或多维list的时候,为了方便阅读而使用了换行缩进写法的话,似乎并不影响代码的含义…… 比如, list1 = [[1,2],[2,3]] 等价于 list1 = [[1,2], [2,3]] print("one is ",one,"\n","two is ",two,"\n",) 等价于 print("one is ",one,"\n", "two is ",two,"\n",) 这种时候,换行缩进的写法跟全写在一行上的写法是等价的么? 如果是,那么在在python里,还有别的什么情况下,换行缩进的写法,也会等价于不换行不缩进的写法呢?
    HmLy5年前 (2019-01-16)回复
  2. 我觉得由缩进来决定逻辑关系,这个并不是好方法,缩进就好好的用来清晰化代码比较好!
    苍苍8年前 (2016-05-12)回复
  3. "Python中的代码缩进,觉得了,不同行代码之间的" 这是不是“决定了”,读起来怪怪的
    vvani10年前 (2014-08-13)回复
    • 恩,谢谢你的提醒,是我笔误。
      crifan10年前 (2014-09-08)回复
95 queries in 0.190 seconds, using 22.18MB memory