From documentation of Naming and Binding -
If a variable is used in a code block but not defined there, it is a free variable.
From the documentation of global statement -
The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without global, although free variables may refer to globals without being declared global.
(Emphasis mine)
When you do -
position[0] = <something>
You are not defining anything, you are mutating the already defined list position
inplace (changing its 0th element to refer to something else) .
Lets assume if position
was not defined ever, when you do something like -
position[0] = 1
You will get NameError
-
>>> position[0] = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'position' is not defined
That is why, even though you do postion[0] = <something>
, its still a free variable, as its not defined in the block, and hence it is treated as global.
UPDATE :
More info , when you do -
position[0] = <something>
This is not a simple assignment
statement , its actually an augmented assignment statement .