[Python-ideas] Add .= as a method return value assignment operator
Steven D'Aprano
steve at pearwood.info
Fri Sep 28 04:10:27 EDT 2018
On Fri, Sep 28, 2018 at 05:34:58PM +1200, Greg Ewing wrote:
> Steven D'Aprano wrote:
> >Think about a more complex assignment:
> >
> > text .= encode(spam) + str(eggs)
>
> I think the only sane thing would be to disallow that, and
> require the RHS to have the form of a function call, which is
> always interpreted as a method of the LHS.
You obviously have a different idea of what is "sane" than I do :-)
But okay, so we cripple the RHS so that it can only be a single method
call. So useful things like these are out:
target .= method(arg) or default
target .= foo(arg) if condition else bar(arg)
and even
target .= method(args) + 1
making the syntax pure sugar for
target = target.method(args)
and absolutely nothing else. I think that's the sort of thing which
gives syntactic sugar a bad name.
The one positive I can see is that if the target is a compound
expression, it could be evaluated once only:
spam[0](x, y, z).eggs['cheese'].aardvark .= method(args)
I suppose if I wrote a lot of code like that, aside from (probably?)
violating the Law of Demeter, I might like this syntax because it
avoids repeating a long compound target.
--
Steve
More information about the Python-ideas
mailing list