On Wed, Sep 26, 2018 at 9:14 PM Jasper Rebane <rebane2...@gmail.com> wrote: > When using Python, I find myself often using assignment operators, like 'a += > 1' instead of 'a = a + 1', which saves me a lot of time and hassle > > Unfortunately, this doesn't apply to methods, thus we have to write code like > this: > text = "foo" > text = text.replace("foo","bar") > # "bar" > > I propose that we should add '.=' as a method return value assignment > operator so we could write the code like this instead: > text = "foo" > text .= replace("foo","bar") > # "bar" > This looks cleaner, saves time and makes debugging easier >
All the other augmented operators are of the form: target #= value with some sort of valid assignment target, and any value at all on the right hand side. You can take the value and put it in a variable, you can replace it with a function returning that value, etc, etc, etc, as it is simply a value. There's no difference between "x += 123" and "y = 123; x += y". (Putting it another way: "x *= y + z" is not equivalent to "x = x * y + z", but to "x = x * (y + z)".) With your proposed ".=" operator, it's quite different: the RHS is textually concatenated with the LHS. This creates odd edge cases. For example: items = ["foo", "bar", "quux"] items[randrange(3)] .= upper() Is this equivalent to: items[randrange(3)] = items[randrange(3)].upper() ? That would call randrange twice, potentially grabbing one element and dropping it into another slot. If it isn't equivalent to that, how is it defined? I'm sure something can be figured out that will satisfy the interpreter, but will it work for humans? ChrisA _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/