本文转载自公众号“读芯术”(ID:AI_Discovery)

织金网站建设公司创新互联,织金网站设计制作,有大型网站制作公司丰富经验。已为织金上千家提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的织金做网站的公司定做!
我常在C / C ++语言中看到很多分号。在代码中,分号表示语句终止。Python并未强制使用分号来分隔语句,但因为分号导致Python乱码的情况可不少。
最近,笔者正在学习Python的数据科学课程。讲师讲解了条件语句并编写了以下代码:
- temp =10;if temp <12: print('It is cold'); cold = True;
笔者当时的反应是:这太可怕了!分号仅在Python中的非典型情况下使用。笔者准备了一篇小指南,解释为什么不应该在Python中使用分号,并列出了少数特殊情况。
语句终止符
[[331088]]
图源:unsplash
在许多大众的编程语言中,需要在每个语句的末尾添加分号。例如,在C ++中:
- int c =10;
- int a = 5;printf('In C++, semicolon at the end is must');
但Python并非如此。Python是一种简洁的编程语言,你不需要添加不必要的字符和语法。在Python中,一条语句结束于一行的结尾(方括号,引号或括号除外)。例如:
- c = 10
- a = 5print('No semicolons in Python')
语句分隔符
Python中的分号表示分隔,而不是终止。它允许在同一行中编写多个语句。
- print('Statement1'); print('Statement 2'); print('Statement 3')
此语法允许在单个语句的末尾加上分号:
- print('WhyGod? WHY?');
该语句表示print('...'),然后在下一句终止。因此它实际上是两个语句,其中第二个为空。即使该语言允许使用分号分隔语句,但大多数Python程序员都不会在其代码中使用它。
[[331089]]
图源:unsplash
Python应该是清晰可读的。分号之类的语法字符会导致不必要的混乱。如果将这样的代码发送给经验丰富的Python程序员,他可能不会再理你了。
- temp =10; cold =False;
- if temp<15: print('It is cold'); cold =True; print('Another statement')
- print(cold); print('Done');
将多条语句强制放在一行上会使原本简单的代码难以阅读。
何时使用分号?
那么问题来了,既然分号既不美观又碍事,为什么Python中允许使用分号?
笔者认为,这是为了使两种编程语言之间的转换变得稍微容易一些。具有Java,C ++和PHP背景的程序员习惯性地将(无用的)终止符放在每一行的末尾。
但是,在下列这些情况下,分号也会派上用场:
从Shell运行脚本
最常见的一种情况是使用python-c'
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-02-16 20:05:18 HTTP/1.1 GET : /article/ccieppg.html
- 运行时间 : 0.0660s ( Load:0.0033s Init:0.0005s Exec:0.0573s Template:0.0049s )
- 吞吐率 : 15.15req/s
- 内存开销 : 2,221.43 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 2 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=ua1nfgjj5mibbjjafg8s8d7vv2
- /home/wwwroot/jxjierui.cn/index.php ( 1.12 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Mode/common.php ( 2.82 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Common/functions.php ( 53.56 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /home/wwwroot/jxjierui.cn/App/Common/Conf/config.php ( 2.12 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/debug.php ( 1.48 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Conf/config.php ( 0.32 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Common/function.php ( 3.33 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php ( 6.11 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /home/wwwroot/jxjierui.cn/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 13.96 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000005s ]
- [ app_init ] --END-- [ RunTime:0.000023s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000146s ]
- [ app_begin ] --END-- [ RunTime:0.000159s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000095s ]
- [ template_filter ] --END-- [ RunTime:0.000113s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.003803s ]
- [ view_parse ] --END-- [ RunTime:0.003818s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000073s ]
- [ view_filter ] --END-- [ RunTime:0.000082s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php 第 47 行.
- [2] file_put_contents(./App/Runtime/Temp/c00dfcde9da1d71e1f4792442b534539.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行.
- [8] Undefined index: db_host /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 122 行.
- [2] file_put_contents(./App/Runtime/Temp/b5d7391a65edc2619dee887bcf9a95f3.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行.

0.0660s
