The question is one of the updates.
You cannot update a because it is not a variable in your local network. In-place update assignment operation is not updated a .
Interestingly, a = a + 1 also fails.
Python generates slightly optimized code for these statements. It uses the LOAD_FAST statement.
2 0 LOAD_FAST 0 (a) 3 LOAD_CONST 1 (1) 6 INPLACE_ADD 7 STORE_FAST 0 (a) 10 LOAD_CONST 0 (None) 13 RETURN_VALUE
Note that using a on the left and right sides of the equal sign leads to this optimization.
However, you can access a because Python will look for local and global namespaces for you.
Since a not displayed on the left side of the assignment operator, another kind of access is used, "LOAD_GLOBAL".
2 0 LOAD_CONST 1 (0) 3 LOAD_GLOBAL 0 (a) 6 LOAD_CONST 2 ('bar') 9 STORE_SUBSCR 10 LOAD_CONST 0 (None) 13 RETURN_VALUE
S. Lott
source share