Search the difference: Why this function defenition does'nt work?

Aahz Maruch aahz at panix.com
Sun Dec 23 19:43:46 CET 2001


In article <3C261778.60108 at student.kun.nl>,
husam  <h.jehadalwan at student.kun.nl> wrote:
>Aahz Maruch wrote:
>> In article <3C260A92.5030907 at student.kun.nl>,
>> husam  <h.jehadalwan at student.kun.nl> wrote:
>>> 
>>>I'm not trying to add string to a list of integeres. What I wanted to do 
>>>is to add each argument of func2('a','b','c') to sum=['e']. Since sum is 
>>>a list of chars, and the arguments are accessable as a sequence, one 
>>>would expect that the addition operation would be correct. On the 
>>>interactive shell, it is a trivial operation. But, only when i use it in 
>>>the function defenition i get problems. Adding 'a' to ['b'] does not 
>>>work, but adding list('a') to ['b'] it does, so, i thought that this 
>>>might help my code, but converting the args to a list did not helpe 
>>>either . I really didn't get yet!
>> 
>> use list.append()
>
>I know that this works, but it does not help my understanding!

What don't you understand?  The '+' operation in Python applies to types
that are more-or-less equivalent.  You can't do this, either:

>>> t='a',
>>> t
('a',)
>>> l=['b']
>>> l
['b']
>>> l+t
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: can only concatenate list (not "tuple") to list

The '+' operation for lists is equivalent to list.extend().
-- 
                      --- Aahz  <*>  (Copyright 2001 by aahz at pobox.com)

Hugs and backrubs -- I break Rule 6                 http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het Pythonista   

Tenth Virtual Anniversary: 8 days and counting



More information about the Python-list mailing list