[Python-ideas] Add .= as a method return value assignment operator

Kirill Balunov kirillbalunov at gmail.com
Thu Sep 27 17:30:59 EDT 2018


As I see it, you are mixing very different things. Augmented operators in
Python work on objects, generally trying to mutate them in-place. So
usually after these operations you have the same object (with the same
type, with the same name and etc.) as before these operations. Of course
there are exceptions, for example all immutable types or some promotions
between numbers.

In your examples some cases imply that you are working on names, others
that you are working on objects. And as for me this ambiguity is not
solvable.

With kind regards,
-gdg

ср, 26 сент. 2018 г. в 14:14, Jasper Rebane <rebane2001 at gmail.com>:

> Hi,
>
> 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
>
> Here are a few more examples:
> text = " foo "
> text .= strip()
> # "foo"
>
> text = "foo bar"
> text .= split(" ")
> # ['foo', 'bar']
>
> text = b'foo'
> text .= decode("UTF-8")
> # "foo"
>
> foo =  {1,2,3}
> bar = {2,3,4}
> foo .= difference(bar)
> # {1}
>
>
> Rebane
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180928/4d3f1cb1/attachment-0001.html>


More information about the Python-ideas mailing list