[Tutor] lists, name semantics

Bill Allen wallenpb at gmail.com
Sun Apr 19 05:03:56 CEST 2015


On Apr 18, 2015 4:11 PM, "boB Stepp" <robertvstepp at gmail.com> wrote:
>
> On Sat, Apr 18, 2015 at 3:28 PM, Bill Allen <wallenpb at gmail.com> wrote:
> > On Apr 18, 2015 7:50 AM, "Peter Otten" <__peter__ at web.de> wrote:
> >
> >> Bill Allen wrote:
> >>
> >> > Everyone that responded,
> >> >
> >> > Thanks very much for the excellent explanations!  The distinction
between
> >> > a reference to an object and a seperate copy of the object is quite
clear
> >> > now.
> >>
> >> You can test your newfound knowledge by predicting the output of the
> >> following script:
> >>
> >>
> >> a = [1, ["x", "y"], 3]
> >> b = a[:]
> >>
> >> a[1][1] = "hello!"
> >>
> >> print(a) # [1, ['x', 'hello!'], 3]
> >> print(b) # what will that print?
> >>
> >> Think twice before you answer. What is copied, what is referenced?
>
> > print(b) will print the original copy of a which b now references which
is
> > [1, ["x", "y"], 3]
>
> Uh, oh! You should have checked your work in the interpreter before
> replying! Peter is being very tricky!! (At least for me...) Look again
> at that list inside of a list and...
>
> boB
>
> P.S.: Watch out for top-posting. That tends to get peopled riled. I
> moved your response back into the normal flow of the interleaved

boB,

Ok, just tried it out.  In this example b=a and b=a[:] seem to yield the
same results even after the change to a, which I do not understand.  Should
not b be a copy of a and not reflect the change?

--bill


More information about the Tutor mailing list