473,473 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Add vs in-place add of str to list

Hi

I'm trying to understand why it is that I can do
>>a = []
a += 'stuff'
a
['s', 't', 'u', 'f', 'f']

but not
>>a = []
a = a + 'stuff'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list

Can someone explain the logic? Why is the in-place add not a type
error but the usual add is? (This applies to both Python 2.6rc1 and
3.0b2)

Thanks
Roman
Oct 2 '08 #1
4 1522
rs387 wrote:
I'm trying to understand why it is that I can do
>>>a = []
a += 'stuff'
a
['s', 't', 'u', 'f', 'f']

but not
>>>a = []
a = a + 'stuff'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list

Can someone explain the logic? Why is the in-place add not a type
error but the usual add is? (This applies to both Python 2.6rc1 and
3.0b2)
It's because the `+=` operator is doing the equivalent of calling the
`extend` method, which treats its argument as a generic sequence, and
doesn't enforce type. The same thing happens with any other sequence
type as the right-hand operand; for instance, tuples:
>>a = []
a += (1, 2, 3)
a
[1, 2, 3]
>>a = []
a = a + (1, 2, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: can only concatenate list (not "tuple") to list

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
Man is a hating rather than a loving animal.
-- Rebecca West
Oct 2 '08 #2
On Oct 2, 8:11*am, Erik Max Francis <m...@alcyone.comwrote:
It's because the `+=` operator is doing the equivalent of calling the
`extend` method, which treats its argument as a generic sequence, and
doesn't enforce type.
I see. Do you know whether this is seen as a problem with the language
design?

If so, this could be fixed by changing list.__add__ to call .extend on
the left argument if it's a list... Or, by changing __iadd__ to throw
a TypeError, whichever way around is seen as more correct/desirable.
Oct 2 '08 #3
Mel
rs387 wrote:
On Oct 2, 8:11Â*am, Erik Max Francis <m...@alcyone.comwrote:
>It's because the `+=` operator is doing the equivalent of calling the
`extend` method, which treats its argument as a generic sequence, and
doesn't enforce type.

I see. Do you know whether this is seen as a problem with the language
design?
No.

[1,2,3] + [4,5,6]

produces

[1,2,3,4,5,6]

not

[1,2,3,[4,5,6]]

Mel.

Oct 2 '08 #4
On Oct 2, 3:50*pm, Mel <mwil...@the-wire.comwrote:
rs387 wrote:
I see. Do you know whether this is seen as a problem with the language
design?

No.
OK, I get it now. I was assuming that the "+" could be implemented in
terms of "+=" as follows:

def add(x,y):
temp = list(x)
temp += y
return temp

But it turns out that this is not what "+" means at all. A bit of a
gotcha...

Roman
Oct 2 '08 #5

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

Similar topics

3
by: Curious Expatriate | last post by:
Hi- I'm completely stumped. I'm trying to write some code that will parse a file and rewrite it with all URLs replaced by something else. For example: if the file looks like this: <b>click...
1
by: JS Bangs | last post by:
I started using PHP's object-oriented stuff a little while ago, which has mostly been a joy. However, I've noticed that they don't seem to echo as I would like. Eg: $this->field = 255;...
5
by: lawrence | last post by:
I've waited 6 weeks for an answer to my other question and still no luck, so let me rephrase the question. I know I can do this: <form method="post" action="$self"> <input type="text"...
0
by: Ben Eisenberg | last post by:
I'm trying to run a php script setuid. I've tried POSIX_setuid but you have to be root to run this. The files are located on a public access unix system and have me as the owner and nobody as the...
2
by: Felix | last post by:
Hi, I've a problem: I want to have the result of my Mysql Query in a Table in my php file. Now I've this: <?
1
by: James | last post by:
What is the best way to update a record in a MYSQL DB using a FORM and PHP ? Where ID = $ID ! Any examples or URLS ? Thanks
1
by: Patrick Schlaepfer | last post by:
Why this code is not working on Solaris 2.8 host. Always getting: PHP Fatal error: swfaction() : getURL('http://www.php.net' ^ Line 1: Reason: 'syntax error' in /.../htdocs/ming2.php on...
1
by: phpkid | last post by:
Howdy I've been given conflicting answers about search engines picking up urls like: http://mysite.com/index.php?var1=1&var2=2&var3=3 Do search engines pick up these urls? I've been considering...
1
by: lawrence | last post by:
What is the PHP equivalent of messaging, as in Java?
3
by: Quinten Carlson | last post by:
Is there a way to conditionally define a function in php? I'm trying to run a php page 10 times using the include statement, but I get an error because my function is already defined. The docs...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.