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

husam h.jehadalwan at student.kun.nl
Sun Dec 23 16:04:59 EST 2001


Aahz Maruch wrote:

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

yes, but the arguments are passed as a tuple and the addition still not 
working even after converting it to a list with: list(x)

your example shows an illegal addition operation, i agree with you, but 
what i'm doing is adding items of a list of chars to sum=['a'], which 
doesn't work only when i do sum=args[0].
I see no difference between sum=['a'] and sum=args[0], simply because 
args[0] is the first index of the arguments list passed to the function. 
args[0] is nothing but one char 'b' or whatever char.




More information about the Python-list mailing list