if session['dp'] := current_user.avatar :
^ SyntaxError: cannot use assignment expressions with subscript
Why Python forbids this use of walrus operator?
Because, as the alternative name (named expressions) suggests, the left hand side of the walrus operator is to be a NAME
. Therefore, by definition such expressions as noted in your question as well as, for instance, function calls are not allowed to be assigned in this form.
The documentation also specifies:
Single assignment targets other than a single
NAME
are not supported
To further this argument, one can notice that cPython explicitly checks if the expression is Name_kind
:
if (target->kind != Name_kind) {
const char *expr_name = get_expr_name(target);
if (expr_name != NULL) {
ast_error(c, n, "cannot use assignment expressions with %s", expr_name);
}
return NULL;
}