详解python中代码缩进(indent)

上传人:第*** 文档编号:30575270 上传时间:2018-01-30 格式:DOCX 页数:12 大小:429.96KB
返回 下载 相关 举报
详解python中代码缩进(indent)_第1页
第1页 / 共12页
详解python中代码缩进(indent)_第2页
第2页 / 共12页
详解python中代码缩进(indent)_第3页
第3页 / 共12页
详解python中代码缩进(indent)_第4页
第4页 / 共12页
详解python中代码缩进(indent)_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《详解python中代码缩进(indent)》由会员分享,可在线阅读,更多相关《详解python中代码缩进(indent)(12页珍藏版)》请在金锄头文库上搜索。

1、【教程】详解 Python 中代码缩进( Indent):影响代码的内在逻辑关系和执行结果2012 年 12 月 24 日 下午 1:49crifan 已有 8165 人围观 2 个评论先给出几个常见的错误和解决办法:Python 中常见的和代码缩进有关的问题IndentationError: unexpected indent举例:这样的代码:# -*- coding: utf-8 -*-import pickleimport mathipath = D:/123fileobj= open(ipath, rb)pdata= pickle.load(fileobj)fileobj.close(

2、)运行结果是:D:tmptmp_dev_rootpythonpick_dump_errorpickle_dump.pyFile D:tmptmp_dev_rootpythonpick_dump_errorpickle_dump.py, line 4ipath = D:/123IndentationError: unexpected indent原因:Python 解析器,发现你的代码缩进有问题。此处的问题是,在import math之后,突然来了个:ipath = D:/123而此种缩进,前面即不是函数定义:def someFunction():xxxxxx也不是其他的形式,所以,语法上,就不

3、支持,即 Python 解析器,不知道这段代码,是属于哪个范围的,无法解析这样的代码。代码执行无结果比如,这样的代码:#!/usr/bin/python# -*- coding: utf-8 -*-Function:【教程】详解 Python 中代码缩进( Indent)http:/ Crifan LiVersion: 2012-12-24Contact: admin at crifan dot comdef indentDemo():print Just demo for python code indent;#if _name_ = _main_:indentDemo();执行出来的结果是

4、空的:D:tmptmp_dev_rootpythonpython_indentpython_indent.pyD:tmptmp_dev_rootpythonpython_indent即,啥都没输出,这和我们要的结果,明显不符。其原因在于:上述代码中的: indentDemo();是属于函数 indentDemo 中的代码,而不是此脚本所能执行出来的代码。而想要达到我们要的效果,即能够执行到对应的函数 indentDemo,可以改为:#!/usr/bin/python# -*- coding: utf-8 -*-Function:【教程】详解 Python 中代码缩进( Indent)http:

5、/ Crifan LiVersion: 2012-12-24Contact: admin at crifan dot comdef indentDemo():print Just demo for python code indent;if _name_ = _main_:indentDemo();或#!/usr/bin/python# -*- coding: utf-8 -*-Function:【教程】详解 Python 中代码缩进( Indent)http:/ Crifan LiVersion: 2012-12-24Contact: admin at crifan dot comdef i

6、ndentDemo():print Just demo for python code indent;#if _name_ = _main_:indentDemo();输出的结果都是:D:tmptmp_dev_rootpythonpython_indentpython_indent.pyJust demo for python code indentD:tmptmp_dev_rootpythonpython_indentpython_indent.py再来解释具体的含义:Python 中的代码缩进Python 中,是通过代码的缩进,来决定代码的逻辑的。通俗的说,Python 中的代码的缩进,不

7、是为了好看,而是觉得代码的含义,上下行代码之间的关系。缩进弄错了,就会导致程序出错,执行结果变成不是你想要的了。关于第一行要执行的代码你写了 Python 代码,如果运行后没有输出你想要的结果,那么很可能是由于你的缩进所导致的。就拿上面的错误例子“ 代码执行无结果 ”来说,实际上, Python 解释器,去执行你的代码的逻辑是: 注:关于_name_和_main_的知识,不了解的去看:【整理】Python 中的_name_和_main_含义详解代码块对应的,上述几个图解中,def indentDemo 后面的代码,也就被因此成为代码块:说白了,就是一个逻辑上的概念,可以简单理解为其他语言中的,

8、一个函数内的代码,一个 if 判断内的代码等等相应的概念;其他语言中的代码缩进:只是决定了是否好看,不影响代码逻辑和运行结果可见,Python 中的代码缩进,觉得了,不同行代码之间的,代码的逻辑关系。而与此相对应的,其他的多数的语言,比如 C,C#,Java 等等,都是通过对应的大括号之类的符号,来决定的代码的逻辑关系的。把上述 Python 代码,写出类似于的 C 等语言的代码,就可以写成:/*Function:【教程】详解 Python 中代码缩进( Indent)http:/ indent in C codeAuthor: Crifan LiVersion: 2012-12-24Cont

9、act: admin at crifan dot com*/#include void indentDemo(void)printf(Just demo for c code indent);int main(void)indentDemo(); return 0;对应的,在 Cygwin 中编译后输出为:CLiPC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/python/python_indent$ gcc c_indent.c -o c_indent.exeCLiPC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/python/pytho

10、n_indent$ ./c_indentJust demo for c code indent上述的代码,很明显,是加了对应的缩进,但是 C 等语言中的缩进,说白了,只是为了代码看起来更“好看”,使得“看起来”代码的逻辑更清晰。实际上,你要是,本来就是,有个不好的编程习惯,或者此处故意地,没有合理的缩进,写出这样:/*Function:【教程】详解 Python 中代码缩进( Indent)http:/ indent in C codeAuthor: Crifan LiVersion: 2012-12-24Contact: admin at crifan dot com*/#include v

11、oid indentDemo(void)printf(Just demo for c code indent);int main(void)indentDemo();return 0;实际上的代码的执行效果,和代码本身的含义,都是没变化的,只是“看起来” ,很不好而已,因为没了合理的,必要的缩进,代码逻辑关系看起来就很不清晰。当然,甚至,此处可以故意改为这种:/*Function:【教程】详解 Python 中代码缩进( Indent)http:/ indent in C codeAuthor: Crifan LiVersion: 2012-12-24Contact: admin at cri

12、fan dot com*/#include void indentDemo(void)printf(Just demo for c code indent);int main(void)indentDemo();return 0;此时,代码的含义本身,仍然是没有变化的,只是,同上,更加不好,代码看起来更加的乱而已。总结:Python 中的代码缩进决定了代码的内在逻辑和执行结果Python 代码缩进,决定了代码的内在逻辑关系,决定了代码的执行结果的对错;相对而言的其他语言,比如 C/C#/Java/,代码缩进,只是为了代码“看起来” 的逻辑更加清晰,但是不影响代码的内在逻辑关系,不影响代码执行结果的。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号