为什么不在Python中使用无用分号
这篇文章主要介绍“为什么不在Python中使用无用分号”,在日常操作中,相信很多人在为什么不在Python中使用无用分号问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”为什么不在Python中使用无用分号”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
成都创新互联是一家从事企业网站建设、成都做网站、网站制作、行业门户网站建设、网页设计制作的专业网站设计公司,拥有经验丰富的网站建设工程师和网页设计人员,具备各种规模与类型网站建设的实力,在网站建设领域树立了自己独特的设计风格。自公司成立以来曾独立设计制作的站点1000+。
我常在C / C ++语言中看到很多分号。在代码中,分号表示语句终止。Python并未强制使用分号来分隔语句,但因为分号导致Python乱码的情况可不少。
最近,笔者正在学习Python的数据科学课程。讲师讲解了条件语句并编写了以下代码:
temp =10;if temp <12: print('It is cold'); cold = True;笔者当时的反应是:这太可怕了!分号仅在Python中的非典型情况下使用。笔者准备了一篇小指南,解释为什么不应该在Python中使用分号,并列出了少数特殊情况。
语句终止符
在许多大众的编程语言中,需要在每个语句的末尾添加分号。例如,在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程序员都不会在其代码中使用它。
图源:unsplashPython应该是清晰可读的。分号之类的语法字符会导致不必要的混乱。如果将这样的代码发送给经验丰富的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-06-03 11:53:50 HTTP/1.1 GET : /article/peojgs.html
- 运行时间 : 1.5299s ( Load:0.0063s Init:0.8682s Exec:0.6466s Template:0.0089s )
- 吞吐率 : 0.65req/s
- 内存开销 : 2,226.82 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 0 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=0m888onb0i6vjmhgupmbdhfns5
- /www/wwwroot/tsicrk.com/index.php ( 1.09 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Mode/common.php ( 2.82 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Common/functions.php ( 53.56 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /www/wwwroot/tsicrk.com/App/Common/Conf/config.php ( 2.14 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Conf/debug.php ( 1.49 KB )
- /www/wwwroot/tsicrk.com/App/Home/Conf/config.php ( 0.31 KB )
- /www/wwwroot/tsicrk.com/App/Home/Common/function.php ( 3.33 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php ( 6.02 KB )
- /www/wwwroot/tsicrk.com/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /www/wwwroot/tsicrk.com/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 17.71 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000005s ]
- [ app_init ] --END-- [ RunTime:0.000027s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000302s ]
- [ app_begin ] --END-- [ RunTime:0.000321s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000050s ]
- [ template_filter ] --END-- [ RunTime:0.000083s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.005972s ]
- [ view_parse ] --END-- [ RunTime:0.005996s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000229s ]
- [ view_filter ] --END-- [ RunTime:0.000279s ]
- [ 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 /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php 第 47 行.
- [8] Undefined index: db_host /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 122 行.

1.5299s
