In pyhon3.10, I can run this code snippet without raising any errors:
{}[0]:1
Which creates an empty dictionary, and then accesses the 0 key. However, the :0
that follows is what I would consider an invalid syntax. And indeed, if I try to determine the type of the result:
type({}[0]:1)
a syntax error gets raised. A similar behavior occurs whenever the I try to work with the result, such as print({}[0]:1)
.
Why does this happen? I am assuming that the interpreter recognizes the expression as not being assigned and does not compile it. And therefore you can run your code with the {}[0]:1
line being present. However, this is not consistent with other syntax errors being raised by different syntactically invalid code (such as 1:1
which raises an error).