This example, together with PEP 736 makes me -1 on this idea. PEP 736 would make it so that foo(x=)
is pure syntactic sugar for foo(x=x)
. With this proposal we could make {x:}
syntactic sugar for {"x": x}
. But then there are two options for something like {2+2:}
.
- Pure syntactic sugar:
{2+2:}
is syntactic sugar for{"2+2": 2+2}
- New syntax checks:
{2+2:}
fails because2+2
is not a valid string of characters for a variable name in python. This would require some change to language parsing I think where a check is done that whatever precedes:
in these cases is a valid variable name. This introduces additional complexity and room for confusion.
In PEP 736 with foo(x=)
the parameter name x
is copied to be the variable name on the right. But both the parameter name and variable name typically obey the same rules. e.g. they cannot start with a number. But in this case we have {x:}
, but now x
is an expression, and it is being used to autocomplete a string {"x": x}
. So different rules need to be obeyed in this case. It’s just different and a little weird.