- 1、本文档共12页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
详解Python中代码缩进(Indent)
【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果/tutorial_python_indent/2012 年 12 月 24 日 下午 1:49/author/crifan/crifan/tutorial_python_indent/已有8165人围观/tutorial_python_indent/2个评论先给出几个常见的错误和解决办法:Python中常见的和代码缩进有关的问题IndentationError: unexpected indent举例:这样的代码:# -*- coding: utf-8 -*-import pickleimport math ipath = D:/123 fileobj= open(ipath, rb) pdata= pickle.load(fileobj) fileobj.close()运行结果是:D:\tmp\tmp_dev_root\python\pick_dump_errorpickle_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)/tutorial_python_indentAuthor:Crifan LiVersion: 2012-12-24Contact: admin at crifan dot comdef indentDemo(): print Just demo for python code indent; #if __name__ == __main__: indentDemo();执行出来的结果是空的:D:\tmp\tmp_dev_root\python\python_indentpython_indent.pyD:\tmp\tmp_dev_root\python\python_indent即,啥都没输出,这和我们要的结果,明显不符。其原因在于:上述代码中的: indentDemo();是属于函数indentDemo中的代码,而不是此脚本所能执行出来的代码。而想要达到我们要的效果,即能够执行到对应的函数indentDemo,可以改为:#!/usr/bin/python# -*- coding: utf-8 -*-Function:【教程】详解Python中代码缩进(Indent)/tutorial_python_indentAuthor: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)/tutorial_python_indentAuthor:Crifan LiVersion: 2012-12-24Contact: admin at crifan dot comdef indentDemo(): print Just demo for python code indent; #if __name__ == __main__:indentDemo();输出的结果都是:D:\tmp\tmp_dev_root\python\python_indentpython_indent.pyJust demo for python code indentD:\tmp\tmp_dev_root\python\python_indentpython_indent.py再来解释具体的含义:Python中的代码缩进Python中,是通过代码的缩进,来决定代码的逻辑的。通俗的说,Python中的
您可能关注的文档
- 建设工程委托监理合同中英.doc
- 重装win7后ubuntu无法启动.doc
- 公众责任保险(94版) - 英文条款.doc
- 林语堂谈如何学英语.doc
- java考试习题9.doc
- GRUB4DOS命令.doc
- 学生机房虚拟化(五)深入配置宿主系统2.doc
- 成功人士是怎样应对愤怒情绪的.docx
- 明智增长术语Smart Growth Glossary.doc
- linux启动引导过程(greb).doc
- 跨境电商物流2025年多式联运供应链优化与成本管理报告.docx
- 专业防水施工合同标准文本.docx
- 跨境电商物流多式联运2025年优化策略与成本控制与物流行业物流行业市场潜力报告[001].docx
- 2025版二手房定金托管合同范本.docx
- 2025年肿瘤早筛技术在肿瘤患者心理康复中的应用前景报告.docx
- 跨境电商物流多式联运2025年优化策略与成本控制市场前景预测[001].docx
- 社区志愿服务在初中生公民教育中的实践路径探析教学研究课题报告.docx
- 跨境电商物流多式联运2025年成本控制与物流服务质量提升策略研究报告.docx
- 跨境电商物流多式联运2025年优化策略及成本控制研究报告[001].docx
- 2025版二手房屋买卖合同.docx
文档评论(0)