Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81495 > unrolled thread

FW: Unexpexted behaviot of python operators on list

Started by"PANDEY2 Archana (MORPHO)" <archana.pandey.2@morpho.com>
First post2014-11-25 10:23 +0100
Last post2014-11-25 13:07 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  FW: Unexpexted behaviot of python operators on list "PANDEY2 Archana (MORPHO)" <archana.pandey.2@morpho.com> - 2014-11-25 10:23 +0100
    Re: FW: Unexpexted behaviot of python operators on list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-25 22:40 +1100
      Re: FW: Unexpexted behaviot of python operators on list Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-11-25 12:20 +0000
        Re: FW: Unexpexted behaviot of python operators on list Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-25 13:07 +0000

#81495 — FW: Unexpexted behaviot of python operators on list

From"PANDEY2 Archana (MORPHO)" <archana.pandey.2@morpho.com>
Date2014-11-25 10:23 +0100
SubjectFW: Unexpexted behaviot of python operators on list
Message-ID<mailman.16274.1416907513.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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

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


Kind Regards


[cid:image001.png@01D008BC.95CE3F20]


Syscom Corporation Limited

Morpho e-Documents Division

D-216, Sector 63, Noida,

Uttar Pradesh, 201301, India








Archana Pandey

Software Engineer-Payment/ID Noida
Tel: +91 (120) 4072 882

Mob: + 91 78 38 207 829

Mail: archana.pandey.2@morpho.com<mailto:archana.pandey.2@morpho.com>




#
" This e-mail and any attached documents may contain confidential or proprietary information. If you are not the intended recipient, you are notified that any dissemination, copying of this e-mail and any attachments thereto or use of their contents by any means whatsoever is strictly prohibited. If you have received this e-mail in error, please advise the sender immediately and delete this e-mail and all attached documents from your computer system."
#

[toc] | [next] | [standalone]


#81499

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-11-25 22:40 +1100
Message-ID<54746a98$0$12990$c3e8da3$5496439d@news.astraweb.com>
In reply to#81495
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

[toc] | [prev] | [next] | [standalone]


#81502

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-11-25 12:20 +0000
Message-ID<mailman.16277.1416918045.18130.python-list@python.org>
In reply to#81499
On 25/11/2014 11:40, Steven D'Aprano wrote:
> 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?
>

http://bugs.python.org/file37269/Operator_bug_python.py

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

x=[1,2,3]
y=x
y=y+[4,5]
print(x)
print(y)

I know that you can explain it better than I can :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#81509

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-11-25 13:07 +0000
Message-ID<m51uvc$kcs$2@dont-email.me>
In reply to#81502
On Tue, 25 Nov 2014 12:20:26 +0000, Mark Lawrence wrote:

> a=[1,2,3]

binds a to the list [1,2,3]

> b=a

binds b to a

> b+=[4,5]

Changes existing b, which is also a

> x=[1,2,3]

binds x to the list [1,2,3]

> y=x

binds y to x

> y=y+[4,5]

Binds y to a new list which comprises previous y plus [4,5]

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web