473,396 Members | 2,037 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,396 software developers and data experts.

a=([],); a[0]+=[1]

When reading about python "warts" i stepped in this
example I find quite puzzling:

a = ( [] , )
a[0] += [1]

this gives an error, because a tuple cannot be
modified. But if you print the content of "a"
it will show it has indeed been modified.

How can this happen ? if "+=" for list was called
then no modification to the tuple was requested and
so no error should have been raised; if instead "+"
and "=" was executed then after a failing assignment
the original list is safe... (or at least I thought
so; if you type "a[0] = [2]" you get the error and
"a" is not modified).

Is there an explanation simpler than investigating
on python sources ?

Andrea
Jul 18 '05 #1
1 1193
Andrea Griffini <ag****@tin.it> wrote in
news:7f********************************@4ax.com:
When reading about python "warts" i stepped in this
example I find quite puzzling:

a = ( [] , )
a[0] += [1]

this gives an error, because a tuple cannot be
modified. But if you print the content of "a"
it will show it has indeed been modified.

How can this happen ? if "+=" for list was called
then no modification to the tuple was requested and
so no error should have been raised; if instead "+"
and "=" was executed then after a failing assignment
the original list is safe... (or at least I thought
so; if you type "a[0] = [2]" you get the error and
"a" is not modified).

Is there an explanation simpler than investigating
on python sources ?


+= is implemented by calling the __iadd__ method (if it exists, otherwise
__add__ is called). In either case the result of the method is then
assigned to the destination.

So what is happening here is that list.__iadd__ modifies the list in place,
but then the attempt to assign the result into the tuple fails.

Although __iadd__ extends the list in place this need not be true for all
types. An object could have an implementation for inplace addition that
actually creates a new object, so Python cannot optimise out the store.

See http://groups.google.co.uk/groups?th...EBE%40noaa.gov
for a (long) discussion of this topic.
Jul 18 '05 #2

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

Similar topics

5
by: Dariusz | last post by:
I have a simple form which I want to try out (before rewriting to do something useful)... and am having problems getting the variable to output. From what I read I have coded it to get around the...
4
by: Dariusz | last post by:
I have constructed a PHP generated HTML layout, which on POSTing the menu option the user selects, calls the same PHP code / page which generates the HTML page and GET's the correct SHTML page into...
3
by: Dariusz | last post by:
I have a problem where I need to pass two variables using GET from a form I have, to solve a page selection problem I have. The code is written that if a new visitor arrives at the front page of...
2
by: g_chime | last post by:
I'm sorry, I had a couple of typos in my original post. Here is the corrected version: How can I make MySQL reject duplicate rows when inserting? The table is quite large: 100+ columns and...
1
by: cincyn12 | last post by:
Hi, I'm really new to access. (Am only a beginner in SQL) I'm trying to create subqueries similar to SQL+ for a sum using case when within date ranges. (Or Decode by month sum) i.e. Trying to...
5
by: mahesh.nimbalkar | last post by:
When I transform XML, XSLT automatically adds extra attributes to the node which are declared in DTD (default DTD attributes) . I just want XSLT not to add these extra default attributes from DTD....
2
by: Ros | last post by:
Hi, I wish to write xml file after validating data from mssql database. I am using xml data mapping list and would use it for validating data. Here is the program : Map_list.xml : <loop...
2
by: Hendrik Schober | last post by:
Hi, the following code is the boiled-down version of something that gives us trouble on GCC. In the real code, 'F' is some fixed-size array class, 'Q' and 'V' are derived classes that probably...
2
by: biofly | last post by:
Hi Friends, I have two tables, second table has relation Table1: ---------- Id ---------- A01 A02
4
by: laguerin | last post by:
I have a subform within a form that lists detail records for a specific account that is selected by a combo box in the forms header. The records contain the type of records (A01, A02, D01, D02 etc...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.