On Wed, Jan 18, 2012 at 4:52 AM, Wilfried Falk <w_h_f...@yahoo.de> wrote: > Hello Pythons, > > attached to this email is a pdf-file which shows, that "+=" does not work > well all along. Mybe somebody of you is able to explain my observations in > this respect. I will be glad about an answer. > > Best regards > Wilfried >
Please do not send attachments to the list. Besides the fact that it's annoying and there's no reason to do it, the list is also available as a Usenet group and I'm pretty sure the gateway doesn't keep attachments. Also, it makes it more difficult for us to respond to you. Common courtesy on this (and many other) groups is to quote the relevant text that we're responding to. It makes it easier for people to follow the conversation, especially when threads get bigger. >Hello Pythons, >my Python is a download of your Python3.2.2 Windows x86 MSI Installer. What I >want to >report is, that "+=" does not work correct all along. >1.) For identifier += 1 you sometimes get print(); identifier = identifier + 1 >What means that there is a prior print(). I could not find out a rule for when >this happens --- but it >does. By replaceing identifier += 1 with identifier = identifier + 1 the >malfunction (print()) allways >disappears. If this was an actual bug in Python, a lot of other people would have noticed it. Since you're the only one with the problem, it probably has something to do with your code and not with Python. Please post an example showing the problem. >2.) Another "mystery" is shown by the code below. There _list += [something] >is not the same >as _list = _list + [something]. > >def conc1(a, _list = []): > _list = _list + [a] > return _list >def conc2(a, _list = []): > _list += [a] > return _list ># Main Program >for i in range(4): > _list = conc1(i) > print(_list) >print() >for i in range(4): > _list = conc2(i) > print(_list) >In the first case the result of print(_list) is: >[0] >[1] >[2] >[3] >In the second case the result of print(_list) is: >[0] >[0, 1] >[0, 1, 2] >[0, 1, 2, 3]. a += b and a = a + b are not the same thing. In Python, a += b is the same thing as a = a.__iadd_(b) while a = a + b is a = a.__add__(b). Like in many other languages, += on objects is an in place operation- it mutates the original object (except in cases of immutable objects like ints). On the other hand, a + b creates a new object with the value of a + b. So in the case of conc1, you're generating a new list each time and leaving the original list (stored in conc1.func_defaults[0] but you shouldn't touch that) alone. In conc2, you're modifying the list so the changed list is shared between calls of the function. -- http://mail.python.org/mailman/listinfo/python-list