Search the difference: Why this function defenition does'nt work?
Steve Holden
sholden at holdenweb.com
Mon Dec 31 14:19:25 EST 2001
"husam" <h.jehadalwan at student.kun.nl> wrote, after much discussion, ...
[ ... ]
> >>>>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!
> >>>>
[Aahz]:
> >>>use list.append()
> >>>
[husam]:
> >>I know that this works, but it does not help my understanding!
> >>
> >
[Aahz]:
> > 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().
> >
[husam]:
>
> 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.
>
What you appear to have failed to notice is that
sum = ['a']
is *not* the same as
sum = args[0]
which is equivalent to
sum = 'a'
Although you are correct in your assumption that strings can be treated as
sequences os characters, this does *not* allow you to mix strings and lists
in the same operation. Therefore, in your second example, when you
initialize your sum using
sum=args[:0]
you are creating a list, containing the first element of the args list. You
can append further elements to this list using sum.append(x), and you can
add other lists using sum = sum + lst. You cannot treat this list containing
a single string element the same as a string. Your second funtion could be
written:
def fun2 (*args):
sum=args[0]
for next in args[1:]
sum=sum+next
return sum
in which case it would return a STRING (the concatenation of all the strings
supplied as arguments), or it could be written
def fun2 (*args):
sum=args[:0]
for next in args[1:]
sum.append(next)
return sum
in which case it would return a LIST containing the elements of the argument
list.
regards
Steve
--
http://www.holdenweb.com/
More information about the Python-list
mailing list