On Fri, Oct 02, 2020 at 11:49:02AM +0900, Stephen J. Turnbull wrote:
-1 for various reasons expressed by several authors. But I'm not sure I agree with this:
Steven D'Aprano writes:
I think this might make good sense for string methods:
mystring = mystring.upper() mystring .= upper()
but less so for arbitrary objects with methods returning arbitrary values, or methods that operate in place.
l =  l *= 5 l
[0, 0, 0, 0, 0]
I'm not sure that I understand your example here. Or rather, I understand your example, I don't understand why you think it disputes my comment above. Your example uses the list repetition operator, which returns a list; it is neither something that returns an arbitrary value, nor a method that operates in place.
Here are two hypothetical examples:
a = [10, 20, 30, 40, 50] a .= index(40) assert a == 3
a = b = [1, 2, 3, 4, 5] a .= reverse() assert b == [5, 4, 3, 2, 1] assert a is None
It's not clear to me that either of these would make good examples of augmented assignment:
- the first replaces the list `a` with an int;
- the second modifies the list in place, and then replaces it with None.
Both are working as designed, in the sense that list.index() returns an int and list.reverse() modifies in place. It might even be that this is precisely what the writer intended. But they are surely *weird* examples for augmented assignment.