[Python-ideas] Add .= as a method return value assignment operator
Jasper Rebane
rebane2001 at gmail.com
Wed Sep 26 07:12:47 EDT 2018
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180926/b7992203/attachment.html>
More information about the Python-ideas
mailing list