有没有办法对Python代码中的数字进行分组以提高代码的可读性?我试过'
and _
which are digit separators of some other languages, but no avail.
一个奇怪的操作符,把它的左手边和右手边连接起来,也可以算出来。
发布于 2018-09-19 13:05:20
几年后更新:Python 3.6现在支持PEP515,因此您可以使用_进行浮点数和整数文字可读性改进。
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1_1000
11000
>>>
对于历史参考,您可以查看严格定义python2.7,python3.5的词法分析...
对于python3.6.0a2及更早版本,您应该收到类似于以下内容的错误消息:
Python 3.6.0a2 (v3.6.0a2:378893423552, Jun 13 2016, 14:44:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1_000
File "<stdin>", line 1
1_000
^
SyntaxError: invalid syntax
>>> amount = 10_000_000.0
File "<stdin>", line 1
amount = 10_000_000.0
^
SyntaxError: invalid syntax
发布于 2018-09-19 14:13:39
Python中没有这样的功能,但建议将其集成到未来。
您可以在PEP515中查看该提案。
https://stackoverflow.com/questions/-100002689
复制相似问题