I am using Python 3.6.1, and I have come across something very strange. I had a simple dictionary assignment typo that took me a long time to find.
context = {}
context["a"]: 2
print(context)
Output
{}
What is the code context["a"]: 2
doing? It doesn't raise a SyntaxError
when it should IMO. At first I thought it was creating a slice. However, typing repr(context["a"]: 2)
raises a SyntaxError
. I also typed context["a"]: 2
in the console and the console didn't print anything. I thought maybe it returned None
, but I'm not so sure.
I've also thought it could be a single line if statement, but that shouldn't be the right syntax either.
Additionally, context["a"]
should raise a KeyError
.
I am perplexed. What is going on?