python needs leaning stuff from other language

Paul McGuire ptmcg at austin.rr.com
Sat Apr 4 10:40:02 EDT 2009


On Apr 3, 11:48 pm, Tim Wintle <tim.win... at teamrubber.com> wrote:
> del mylist[:]
> * or *
> mylist[:] = []
> * or *
> mylist = []
>
> which, although semantically similar are different as far as the
> interpreter are concerned (since two of them create a new list):
>

Only the last item creates a new list of any consequence.  The first
two retain the original list and delete or discard the items in it.  A
temporary list gets created in the 2nd option, and is then used to
assign new contents to mylist's [:] slice - so yes, technically, a new
list *is* created in the case of this option. But mylist does not get
bound to it as in the 3rd case.  In case 2, mylist's binding is
unchanged, and the temporary list gets GC'ed almost immediately.

-- Paul



More information about the Python-list mailing list