with open('/dev/null') as x^y|z: pass
Yes, this code is valid according to the grammar! Otherwise, you will receive a parsing error ("invalid syntax"). The native parser is good with this syntax, it is another part of the compiler that checks that such an expression is not allowed on the left side (because as semantically equivalent to the destination). The reason the grammar allows expr here, and not just NAME is because you can get any l as value:
with open('/dev/null') as some.thing with open('/dev/null') as some[thing]
but there is no separate rule for lvalues, for example. assignments use testlist on the left, which is even wider than expr .
georg source share