I accidentally used :
instead of =
when assigning a variable, and I was surprised that it didn't generate an error. For example, the following runs without complaints:
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'
>>>
However, this doesn't seem to actually do anything. I've tried looking in the documentation and tutorials, I only find compound statements, dict comprehensions and sequence slicing, and I can't see how either of those apply in this case.
For comparison, in Python 2.7 it does generate a syntax error:
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'
^
SyntaxError: invalid syntax
Why does this not cause a syntax error in Python 3.7, and what (if anything) could it be used for?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…