Why is this?

Mike Meyer mwm at mired.org
Fri Aug 12 21:03:21 EDT 2005


Peter Mott <peter at monicol.co.uk> writes:

> If I use concatenation + instead of multiplication * then I get the
> result that Jiri expected:
>
>  >>> L = [[]] + [[]]
>  >>> L[1].append(1)
>  >>> L
> [[], [1]]
>
> With * both elements are changed:
>
>  >>> L = [[]] * 2
>  >>> L[1].append(1)
>  >>> L
> [[1], [1]]
>
> Alex Martelli says in his excellent Nutshell book that + is
> concatenation and that "n*S is the concatenation of n copies of
> S". But it seems not so. Surely, from a logical point of view, S + S
> should be the same as S * 2?

Only if you think lists are numbers. They aren't. The only real
resemblance between the operators on lists and numbers is that the
symbols got overloaded for both types. The operators on lists aren't
commutative, aren't associative, and don't have inverses. Given that,
the surprising thing is that S + S is the same as S * 2 as often as it
is.

           <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list