list.append problem?

Steve Holden sholden at holdenweb.com
Tue Oct 2 08:56:21 EDT 2001


"Bob Parnes" <rparnes at megalink.net> wrote ...
> Bob Parnes wrote:
[ ... ]
> > As another newcomer I discovered this on my own. What is confusing, I
> > think, is that it seems to apply only to empty objects. For example
> >
> >>>> a = b = [1, 2. 3]
> >>>> a = [4, 5, 6]
> >>>> b
> > [1, 2, 3]
> >>>> a.append(7)
> >>>> a
> > [4, 5, 6, 7]
> >>>> b
> > [1, 2, 3]
> >
> > In this case a and b are not bound to the same object. It seems to me
that
> > python treats empty objects differently from assigned objects. Or
> > something like that, I'm probably not articulating it well.
> >
>
> Sorry, I wrote too hastily
>
> >>> a = b = [1, 2, 3]
> >>> a.append(4)
> >>> a
> [1, 2, 3, 4]
> >>> b
> [1, 2, 3, 4]
>
> The rule is the same, I used the wrong example.
>
Yup. In your first example you explicity rebound the name a, meaning that
you could no longer use it to see changes to the original object, still
bound to the name b.

Once a beginner realises that all names in Python are essentially object
references, this style becomes quite natural.

Nice to see I'm no the only person to reply to my own posts!

regards
 Steve
--
http://www.holdenweb.com/






More information about the Python-list mailing list