On Sat, Oct 24, 2020 at 09:26:23PM -0000, Joseph Martinot-Lagarde wrote:
Steven D'Aprano wrote:
# Dotted names from types import SimpleNamespace obj = SimpleNamespace() obj.spam = **{'obj.spam': 1} assert obj.spam == 1
# Subscripts arr = [None]*5 arr[1], arr[3] = **{'arr[3]': 33, 'arr[1]': 11} assert arr == [None, 11, None, 33, None]
Currently in Python `arr[1]` is the same as `arr[ 1 ]` (notice the added spaces). How is it taken into account in you proposal, does one match and the other doesn't ? Are those line equivalent or not :
Good question! On the left hand side of the assignment, the target follows the normal Python rules so spaces within the subscript disappear when the code is compiled: >>> dis.dis('arr[1]') 1 0 LOAD_NAME 0 (arr) 2 LOAD_CONST 0 (1) 4 BINARY_SUBSCR 6 RETURN_VALUE >>> dis.dis('arr[ 1 \t ]') 1 0 LOAD_NAME 0 (arr) 2 LOAD_CONST 0 (1) 4 BINARY_SUBSCR 6 RETURN_VALUE On the right hand side of the assignment, the situation is a little more complicated because the keys are strings. The key matching would have to use the same rules as the interpreter, so that all of these keys would be treated identically: 'arr[1]' 'arr[ 1 ]' 'arr [ 1 ]' 'arr[(1)]' # but not 'arr[(1,)]' as that would be a tuple subscript etc. This adds some complexity to the process, not quite as simple as a naive string comparison, but shouldn't be too difficult. -- Steve