4 ms·
>>> a := "wtf_walrus" File "<stdin>", line 1 a := "wtf_walrus" ^ SyntaxError: invalid syntax Why doesn't this work? Isn't the walrus
by CivBase 6y ago
>>> a := "wtf_walrus"
File "<stdin>", line 1
a := "wtf_walrus"
^
SyntaxError: invalid syntax
Why doesn't this work? Isn't the walrus operator effectively just an alternative to the assignment operator which also returns the assigned value? Why specifically make it fail in this situation?
- legobmw99 6y agoFrom the PEP [1] > This rule is included to simplify the choice for the user between an assignment statement and an assignment expression -- there is no syntactic position where both are valid. 1: https://www.python.org/dev/peps/pep-0572/ https://www.python.org/dev/peps/pep-0572/
- CivBase 6y agoAh. I forgot about the "one right way" philosophy. But if there is no syntactic position where both are valid, then why not just add a return value to the existing assignment operator? Surely that wouldn't cause any compatibility issues since prior to its introduction any attempt to use an assignment as part of an expression would raise a syntax error.