While experimenting with list insertion/concatenation by slicing, I entered this typo at Python 3.6.4 interactive prompt: x:[-9999] = [-1, 0]
, intending x[:-9999] = [-1, 0]
as an exploratory way of inserting [-1, 0] at the beginning of a list. My typo caused x to be assigned the list [-1, 0]. Surprised, I tried similar expressions, like x:5=1
, which assigns 1 to x, and x:’hello’=‘goodbye’
, which assigns ‘goodbye’ to x. This is either obscure syntax or very simple, but why does this expression work, as if the colon and what immediately follows it weren’t there at all? I’m guessing it’s some kind of mapping or substitution prior to assignment?
Asked
Active
Viewed 4,716 times
4

Stephen Frost
- 218
- 4
- 13
-
1This is type annotation syntax! Nice. – Ry- Jan 20 '18 at 06:43
-
https://www.python.org/dev/peps/pep-0526/ – Ry- Jan 20 '18 at 06:44