小编Joh*_*röm的帖子

这段代码中的冒号 (:) 有什么作用?

我在分配变量时不小心使用了:而不是=,我很惊讶它没有产生错误。例如,以下运行没有任何抱怨:

Python 3.7.4 (default, Jul  9 2019, 18:15:00)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a: 'hello world'
>>>
Run Code Online (Sandbox Code Playgroud)

然而,这似乎实际上并没有做任何事情。我试过查看文档和教程,我只找到复合语句字典理解序列切片,我看不出它们中的任何一个如何适用于这种情况。

为了进行比较,在 Python 2.7 中它确实会产生一个语法错误:

Python 2.7.16 (default, Apr 12 2019, 15:32:52)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a: 'hello world'
  File "<stdin>", line 1
    a: 'hello world' …
Run Code Online (Sandbox Code Playgroud)

python-3.x

3
推荐指数
1
解决办法
69
查看次数

标签 统计

python-3.x ×1