FW: Unexpexted behaviot of python operators on list

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Nov 25 06:40:07 EST 2014


PANDEY2 Archana (MORPHO) wrote:

> Hello
> 
> I hereby would like to share the problem I have found regarding python
> list implementation:-
> 
> As per python documentation python list is mutable data object.
> 
> That problem I found with the list is that is behaves differently when we
> use '+=' and '+'  '=' operators separately. For example-
> a=a+1 and a +=1  both behave in same way for all data types except python
> list

`a += b` is only *approximately* the same as `a = a+b`. The documentation
says:

An augmented assignment expression like x += 1 can be rewritten as x = x + 1
to achieve a similar, but not exactly equal effect. In the augmented
version, x is only evaluated once. Also, when possible, the actual
operation is performed in-place, meaning that rather than creating a new
object and assigning that to the target, the old object is modified
instead.

https://docs.python.org/2/reference/simple_stmts.html#augmented-assignment-statements


> Please find the attached module and execute it on windows python32, See
> the difference in output.

I cannot see the attached module. Did you forget to attach it, or did your
mail server delete it?




-- 
Steven




More information about the Python-list mailing list