The for loop follows standard assignment rules, so what works on the vanilla name LHS should work with for :
Each item is in turn assigned to the target list using the standard assignment rule.
The for construct simply invokes the fundamental target assignment mechanism, which in the case of your STORE_SUBSCR sample code:
>>> foo = [42] >>> k = {'c': 'd'} >>> dis.dis('for k["e"] in foo: pass') 1 0 SETUP_LOOP 16 (to 18) 2 LOAD_NAME 0 (foo) 4 GET_ITER >> 6 FOR_ITER 8 (to 16) 8 LOAD_NAME 1 (k) 10 LOAD_CONST 0 ('e') 12 STORE_SUBSCR <-------------------- 14 JUMP_ABSOLUTE 6 >> 16 POP_BLOCK >> 18 LOAD_CONST 1 (None) 20 RETURN_VALUE
But, to my surprise, it was not a syntax error
Apparently everything that works on a regular basis, such as:
full purpose of slice :
>>> for [][:] in []: ... pass ... >>>
list subscription
>>> for [2][0] in [42]: ... pass ... >>>
dictionary subscription, etc. will be valid target candidates, with a single exception being a tied destination ; although, I secretly think that you can prepare some kind of dirty syntax to execute the chain.
I would expect only single identifiers and identifier tuples
I cannot come up with a good use case for a dictionary key as a goal. In addition, it is more readable to perform the assignment of a dictionary key in the body of a loop than to use it as a target in a for clause.
However, advanced unpacking (Python 3), which is very useful with regular assignments, is equally useful in a for loop:
>>> lst = [[1, '', '', 3], [3, '', '', 6]] >>> for x, *y, z in lst: ... print(x,y,z) ... 1 ['', ''] 3 3 ['', ''] 6
The corresponding mechanism of appointment for different goals of the target is also called; multiple STORE_NAME s:
>>> dis.dis('for x, *y, z in lst: pass') 1 0 SETUP_LOOP 20 (to 22) 2 LOAD_NAME 0 (lst) 4 GET_ITER >> 6 FOR_ITER 12 (to 20) 8 EXTENDED_ARG 1 10 UNPACK_EX 257 12 STORE_NAME 1 (x) <----- 14 STORE_NAME 2 (y) <----- 16 STORE_NAME 3 (z) <----- 18 JUMP_ABSOLUTE 6 >> 20 POP_BLOCK >> 22 LOAD_CONST 0 (None) 24 RETURN_VALUE
Shows that a for are just simple assignment statements executed in sequence.