The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

Jon Ribbens jon+usenet at unequivocal.co.uk
Thu Mar 24 15:07:42 EDT 2016


On 2016-03-24, Steven D'Aprano <steve at pearwood.info> wrote:
> On Fri, 25 Mar 2016 02:03 am, Jon Ribbens wrote:
>> On 2016-03-24, BartC <bc at freeuk.com> wrote:
>>> On 24/03/2016 14:08, Jon Ribbens wrote:
>>>> if you thought you needed to do that then most likely what you
>>>> should actually be doing is re-writing your code so you no longer
>>>> need to. However, you could do:
>>>>
>>>>    L[:] = [0] * len(L)
>>>
>>> OK, but that's just building a new list as I've already mentioned.
>> 
>> No it isn't, it's replacing the elements in-place, that's what the
>> "L[:]" bit means. Replacing the list object itself would be:
>> L = [0] * len(L)
>
> Yes it is: a new list is built, copied, then discarded.

Obviously (as already mentioned in my earlier reply). But the point is
that it is not "just building a new list", as if that were true then L
would not be pointing to the same list afterwards.



More information about the Python-list mailing list