473,385 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

User-defined augmented assignment

Hello,

a discussion began on python-dev about this. It began by a bug report,
but is shifted and it now belongs to this discussion group.

The problem I find with augmented assignment is it's too complex, it's
badly explained, it's error-prone. And most of all, I don't see any
use-case for it !

The most common error is to consider that :

a += b <==> a.__iadd__(b)

when the truth is :

a += b <==> a = a.__iadd__(b)

which can be very confusing, as the two "a" are not necessarily the
same. It then leads to subtle errors like:
class A(object):
a = 0 a = A()
b = A()
a.a += 1
A.a += 2
print a.a 1 print b.a 2

Also, the following behavior is pretty confusing :
a = [1]
b = [a]
c = (a,)
b[0] += [2] # Ok, no pb
print a [1,2] c[0] += [3] Traceback (most recent call last):
File "<string>", line 1, in ?
TypeError: object doesn't support item assignment print a

[1,2,3]

Then, in the standard library, there is no use-case of user-defined
augmented assignment I could find. Of course, I find the augmented
assignement itself very useful ! I use it a lot with immutable objects
(strings, numbers, tuples, ...) but I tend to avoid it with mutables,
and so it seems in the standard library that uses extensively the
"extend" method of lists and very seldom the "+=" operator with lists.
And even where the "a+=b" is used, it could be replaced with either
"a.extend(b)" or "a = a+b" without bugs.

So, what I would suggest is to drop the user-defined augmented
assignment and to ensure this equivalence :

a X= b <=> a = a X b

with 'X' begin one of the operators.

Pierre
Sep 29 '05 #1
4 1513
Pierre Barbier de Reuille wrote:
So, what I would suggest is to drop the user-defined augmented
assignment and to ensure this equivalence :

a X= b <=> a = a X b

with 'X' begin one of the operators.


It can be done, but it's unnecessary for mutable objects like
sets or lists. A new object must be created in these cases where
one would suffice.

Reinhold
Sep 29 '05 #2
Reinhold Birkenfeld a écrit :
Pierre Barbier de Reuille wrote:

So, what I would suggest is to drop the user-defined augmented
assignment and to ensure this equivalence :

a X= b <=> a = a X b

with 'X' begin one of the operators.

It can be done, but it's unnecessary for mutable objects like
sets or lists. A new object must be created in these cases where
one would suffice.


Well, my point is: the benefit is too small compared to the
disadvantage. If you really have a mutable (let say a list with +=) then
you do:
a.extend(b)


and there is no interpretation error possible. BTW, that's what's done
in the standard library ...

Pierre

Reinhold

Sep 29 '05 #3
I thought along these lines:
It is an augmented ASSIGNMENT. (It even has an equals sign in it).
tuples are immutable so you should not be able to assign to one of
its elements.

- So there is no problem for me - I shouldn't be messing with an
element of an
immutable type!

- Cheers, Paddy.

Sep 29 '05 #4
On Thu, 29 Sep 2005, Pierre Barbier de Reuille wrote:
a discussion began on python-dev about this. It began by a bug report,
but is shifted and it now belongs to this discussion group.

The problem I find with augmented assignment is it's too complex, it's
badly explained, it's error-prone. And most of all, I don't see any
use-case for it !

The most common error is to consider that :

a += b <==> a.__iadd__(b)

when the truth is :

a += b <==> a = a.__iadd__(b)

which can be very confusing, as the two "a" are not necessarily the
same.
Indeed. I certainly didn't realise that was how it worked.
So, what I would suggest is to drop the user-defined augmented
assignment and to ensure this equivalence :

a X= b <=> a = a X b

with 'X' begin one of the operators.


That seems quite an odd move. Your proposal would lead to even more
surprising behaviour; consider this:

a = [1, 2, 3]
b = a
a += [4, 5, 6]
print b

At present, this prints [1, 2, 3, 4, 5, 6]; if we were to follow your
suggestion, it would be [1, 2, 3].

So, -1, i'm afraid.

I think the right solution here is staring us in the face: if everyone
seems to think that:

a += b <==> a.__iadd__(b)

Then why not make it so that:

a += b <==> a.__iadd__(b)

Principle of Least Surprise and all that.

Since not everything that should support += is mutable (integers, for
example), how about saying that if the recipient of a += doesn't have an
__iadd__ method, execution falls back to:

a = a + b

I say 'a + b', because that means we invoke __add__ and __radd__
appropriately; indeed, the __add__ vs __radd__ thing is a precedent for
this sort of fallback.

Doesn't that leave everyone happy?

tom

--
I'm not quite sure how that works but I like it ...
Oct 2 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

60
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm ...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
7
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the...
0
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
2
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
1
by: Carlettus | last post by:
Dear All, sorry but I'm not sure if this is the right place to post my problem. I was using the following asp code to create users in Active Directory. Suddenly, and I don't know the reason, users...
0
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have...
9
by: Gordon | last post by:
I want to add a feature to a project I'm working on where i have multiple users set up on my Postgres database with varying levels of access. At the bare minimum there will be a login user who...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.