Consider the following code snippet:
dict [name] = 0
dict [name] += 1
dict [name] += 1
Does the python interpreter automatically recognise the repeated references to the dictionary value and use a cached local reference instead?, somewhat akin to the aliasing optimisations of C/C++, becoming something like so:
value = dict [name]
value = 0
value += 1
value += 1
Obviously, it’s not a big deal to do this manually but I’m curious if it’s actually necessary. any insight, feedback, etc is appreciated.
You can run it through the disassembler to find out:
Running this we get:
It looks like, in this case, that
LOAD_FASTis loading up the values oftdictandnameeach time we try to access it to perform the increment, so the answer would appear to be no.