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


Groups > comp.lang.python > #81504

Re: FW: Unexpexted behaviot of python operators on list

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'operator': 0.03; 'differently': 0.07; 'welcome.': 0.07; '[1,': 0.09; 'happen,': 0.09; 'immutable': 0.09; 'second.': 0.09; 'python': 0.11; 'changes': 0.15; 'windows': 0.15; "'+'": 0.16; 'behave': 0.16; 'behavior,': 0.16; 'list),': 0.16; 'mutable': 0.16; 'subject:operators': 0.16; 'types,': 0.16; 'subject:python': 0.16; 'attach': 0.16; 'wrote:': 0.18; 'module': 0.19; 'trying': 0.19; 'properly': 0.19; 'header:User-Agent:1': 0.23; 'certainly': 0.24; 'text,': 0.24; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'subject:list': 0.30; "i'm": 0.30; 'code': 0.31; 'apparently': 0.31; 'object.': 0.31; 'operators': 0.31; 'quite': 0.32; 'text': 0.33; 'guess': 0.33; 'problem': 0.35; 'except': 0.35; 'test': 0.35; 'module.': 0.36; 'object,': 0.36; 'possible': 0.36; 'list': 0.37; 'implement': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'mailing': 0.39; 'either': 0.39; 'according': 0.40; 'tell': 0.60; 'affect': 0.61; 'forum': 0.61; 'new': 0.61; 'first': 0.61; 'received:74.208': 0.68; 'special': 0.74; 'behavior': 0.77; 'archana': 0.84; 'mailer': 0.84; 'received:74.208.4.194': 0.84
Date Tue, 25 Nov 2014 07:25:49 -0500
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0
MIME-Version 1.0
To python-list@python.org
Subject Re: FW: Unexpexted behaviot of python operators on list
References <7E0894E94554B548A261863FD8CE7824479D97FAE5@Y000XAKV01.rd1.rf1>
In-Reply-To <7E0894E94554B548A261863FD8CE7824479D97FAE5@Y000XAKV01.rd1.rf1>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:3wgPnD91Gid2oopmDdhUl/NAjEejLCKzqXa5b1JAtj0 9OeLkHIY6aJOyWMRZ6bcm06HcXae/mFGQ1waAMl67X4kxeJvqa LvnB+jg1l1TmVWIkYFGqNQOCipEiX+ZS839gOKmRefv/WjVvxY 4kEPZvUD/H6AyIFm35Yz75/WXtOUmkk6HtdhLtsBT8L7VaZORG TbxxTUurbg9Zusmt36xdczG/+hbfe4Wr/Y0mjwqk7ep5Hr1t0G ZdLFjP9A7qOkbFfnMw2FavPTsiFKvSdnZzW0BBNnGMg4Wd71lf J78UNdAwizTSyh8seSp5tw7szSg1PEPxAo40eWHnn97f7IC2aQ cIK29v8Qa3G2OJYjKd4E=
X-UI-Out-Filterresults notjunk:1;
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16279.1416918368.18130.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416918368 news.xs4all.nl 2854 [2001:888:2000:d::a6]:48518
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81504

Show key headers only | View raw


On 11/25/2014 04:23 AM, PANDEY2 Archana (MORPHO) wrote:
> Hello
>

Welcome.  This is apparently your first post, or at least first for 
quite a while.

Note that this is a text forum (usenet, mailing list), and doesn't 
properly support either html messages nor attachments.  Just tell your 
mailer to use text, and paste your problem code into your message 
instead of trying to attach it.

> 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.
>

I'm going to guess what your problem is, without seeing your module.

list1 = list2 = [1, 3, 5]
list1 += [7]
print(list1, list2)

#The += operator will affect list 2, not just list1.

list1 = list2 = [1, 3, 5]
list1 = list1 + [7]
print(list1, list2)

#The + operator makes a new object, and binds it to list1, leaving
#   list2 unchanged.

This is exactly according to spec, and will work the same for any 
mutable type.  For immutable types, the first behavior cannot happen, so 
it changes to the second.

It's certainly possible some other mutable types will behave 
differently, as each object type can implement its own behavior, based 
on its special methods.

No time right now to test this, so apologies if I have typos.





-- 
DaveA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: FW: Unexpexted behaviot of python operators on list Dave Angel <davea@davea.name> - 2014-11-25 07:25 -0500

csiph-web