C
Carl Banks
I don't understand your reasoning. If assignment operators are so
terrible, why do you think the terribleness disappears in this specific
case?
I don't.
The assignment operator is terrible but the idiom itself isn't.
[snip narrowly applicable counterexamples that don't really mean
anything because they're too narrow]
Perl's treatment of assignment as an operator tempts the programmer to
write quick-and-dirty code.
(The perl example wasn't using an assignment operator.)
Python discourages that sort of behaviour,
and encourages programmers to factor their code in a structured way.
That's a feature, not a bug.
The set-and-test idiom is not necessarily quick and dirty, and for
many things it's more readable and a lot easier to follow than any of
indirect methods that are proposed.
The fact that sometimes an assignment operator is used to do this
doesn't change the usefulness of the idiom itself.
It can be done without assignment expressions. I gave a hypothetical
syntax for how it might be done in Python without them.
I can't help but to think that a lot of people's distaste for this
natural way to write certain logic is simply defensiveness about one
minor little thing that Python doesn't support (without workarounds).
Carl Banks