[Python-ideas] Assign-in-place operator

Steven D'Aprano steve at pearwood.info
Tue Jun 4 07:29:17 EDT 2019


On Tue, Jun 04, 2019 at 12:47:30PM +0200, Jeroen Demeyer wrote:

> When you think of it this way, it's not an unreasonable request. There 
> would be at least one major use of this operator within CPython, for 
> lists. With this proposal, the awkward syntax (there are 219 instances 
> of this in the CPython sources)
> 
>   L[:] = new_list

What is so awkward about slice assignment? It is an obvious 
generalisation of item assignment to slices of more than one index, with 
the start and end positions being optional.

If you can use ``L[index] = value`` than slice assignment just follows 
from that.


> would become
> 
>   L <== new_list

Creating new syntax to make it easy to do things which are currently 
impossible or difficult is worth considering; creating new syntax just 
because some people don't like the colour of the bike-shed just creates 
language churn for its own sake.

Introducing <== to give alternate syntax to slice assignment is, I 
think, a non-starter.


> The implementation would be completely analogous to the existing 
> in-place arithmetic operators. For example A <== B would become 
> equivalent to A = type(A).__iassign__(A, B).

As far as I can tell, there is no difference between your proposal 
and the OP's proposal except you have changed the name of the dunder 
from __arrow__ to __iassign__.

__iassign__ is inappropriate because there is no __assign__ dunder:

    x += y __iadd__ is related to x + y __add__
    x -= y __isub__ is related to x - y __sub__
    x *= y __imul__ is related to x * y __mul__
    # etc 
    x <== y __iassign__ is related to x <what?> y __assign__ ?

and it is not a form of *augmented assignment*, it's just a method call.



-- 
Steven


More information about the Python-ideas mailing list