[Python-ideas] Add .= as a method return value assignment operator
brenbarn at brenbarn.net
Thu Sep 27 22:50:06 EDT 2018
On 2018-09-27 03:48, Ken Hilton wrote:
> Would there be a dunder method handling this? Or since it's explicitly
> just a syntax for "obj = obj.method()" is that not necessary?
> My only qualm is that this might get PHP users confused; that's really
> not an issue, though, since Python is not PHP.
I'm opposed to this idea, precisely because there's no way to make a
dunder for it. Exiting augmented assignment operators work like this:
"LHS op= RHS" means "LHS = LHS.__iop__(RHS)" (where iop is iadd, isub,
This can't work for a dot operator. If "LHS .= RHS" is supposed to
mean "LHS = LHS.RHS", then what is the argument that is going to be
passed to the dunder method? The proposed .= syntax is using the RHS as
the *name* of the attribute to be looked up, but for all existing
augmented assignments, the RHS is the *value* to be operated on.
As others pointed out elsewhere in the thread, the problem is
compounded if there are multiple terms in the RHS. What does "this .=
that + other" mean? What would be the argument passed to the dunder
function? Is the name of the relevant attribute supposed to be taken
from "that" or "other" or from the result of evaluating "that + other"?
I like the consistency of the existing augmented assignment operations;
they are just all syntactic sugar for the same pattern: LHS =
LHS.__iop__(RHS) . I'm opposed to the creation of things that look like
augmented assignment but don't follow the same pattern, which is what
this proposal does.
"Do not follow where the path may lead. Go, instead, where there is no
path, and leave a trail."
More information about the Python-ideas