3 ms·
This honestly makes it seem more confusing to me. The fact that there is now an operator that can only be used in certain statements just makes things more conf
by txcwpalpha 7y ago
This honestly makes it seem more confusing to me. The fact that there is now an operator that can only be used in certain statements just makes things more confusing. And if there really is no overlap, then why wasn't the "=" operator just extended to also work in expressions? "while chunk = read()" seems like it makes just as much sense without adding the confusion of another operator.
- ericdevries 7y agoOne of the good things about not using the "=" operator is that you cannot accidentally turn a comparison into an assignment, a feature that is a common cause of errors in other languages that do support it. By adding a completely different character to the operator it is not very likely to cause bugs, compared to just forgetting to type that second =
- bluecalm 7y agoIs it really that common? I made this typo a few times in my life. It was corrected every time before the program actually run because the compiler warned me about it. I don't see how you can make this mistake if you're not aggressively trying (by turning off warnings for example).
- ericdevries 7y agoI guess it is not common, but by using the = operator you would not get the warning, and instead get unexpected behaviour.
- RaptorJ 7y agoI expect the PEP authors want to avoid the "while chunk == read()" class of bugs ninjaedit: indeed https://www.python.org/dev/peps/pep-0572/#why-not-just-turn-existing-assignment-into-an-expression https://www.python.org/dev/peps/pep-0572/#why-not-just-turn-...
- benchaney 7y agoAnd also the converse (but no less dangerous) "if value = true"
- dragonwriter 7y ago> The fact that there is now an operator that can only be used in certain statements just makes things more confusing The new operator (like many Python operators) can only be used in expressions (statements can contain expressions, but expressions are not a subset of statements.) > The fact that there is now an operator that can only be used in certain statements just makes things more confusing Because the “=” operator is the thing that defines an assignment statement. Even if this could be resolved unambiguously for language parsers, so that “statement defining” and “within expression” uses didn't clash, it would be create potential readability difficulties for human reading. Keeping them separate makes the meaning of complicated assignment statements and assignment-including expressions more immediately visually clear.
- txcwpalpha 7y ago>it would be create potential readability difficulties for human reading I think the major argument (at least, the one I see most frequently) is that the walrus operator does create readability difficulties for humans, which is exactly why many people view it as non-pythonic. This is one of the few times I've seen someone argue that ":=" makes things more readable.
- dragonwriter 7y agoAn argument against expression assignment is that it can create readability problems compared to separating the assignment from the expression in which the value is used. Even most supporters of the feature agree that this can be true in many cases and that it should be used judiciously. This is in no way contrary to the argument that the walrus operator improves readability of expression assignments compared to using the same operator that defines assignment statements.