initialized list: strange behavior

Benjamin Kaplan benjamin.kaplan at case.edu
Tue Nov 25 09:31:10 EST 2008


On Tue, Nov 25, 2008 at 9:23 AM, Arnaud Delobelle <arnodel at googlemail.com>wrote:

> Jason Scheirer <jason.scheirer at gmail.com> writes:
>
> > On Nov 24, 10:34 pm, alexander.gen... at gmail.com wrote:
> >> Hi Python experts! Please explain this behavior:
> >>
> >> >>> nn=3*[[]]
> >> >>> nn
> >> [[], [], []]
> >> >>> mm=[[],[],[]]
> >> >>> mm
> >>
> >> [[], [], []]
> >>
> >> Up till now, 'mm' and 'nn' look the same, right? Nope!
> >>
> >> >>> mm[1].append(17)
> >> >>> mm
> >> [[], [17], []]
> >> >>> nn[1].append(17)
> >> >>> nn
> >>
> >> [[17], [17], [17]]
> >>
> >> ???
> >>
> >> Python 2.5 Win XP
> >>
> >> Thanks!
> >
> > You're creating three references to the same list with the
> > multiplication operator.
>
> There's no need to introduce references: you're creating a list with the
> same object at each position.
>
> [...]
> > Python is pass-by-reference, not pass-by-value.
>
> It's certainly not pass-by-reference, nor is it pass-by-value IMHO.


Please don't get into this here. We have enough threads for this already.


>
>
> --
> Arnaud
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081125/844e8a6c/attachment.html>


More information about the Python-list mailing list