which methods to use?

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Thu Mar 29 01:17:00 EDT 2007


On Thu, 29 Mar 2007 04:49:17 +0000, Dennis Lee Bieber wrote:

> On 28 Mar 2007 18:16:31 -0700, s99999999s2003 at yahoo.com declaimed the
> following in comp.lang.python:
> 
> 
>> what do you mean by create new object when using list comprehensoin or
>> list()? Does using slicing create a new object as well?
> 
> 	If the destination is a bare name, YES.
> 
> 	If the destination is a slice of a list, then one is merely
> replacing the contents in that slice, but leaving the other parts of the
> list in place.

Maybe so, but the slice doesn't know where it is being assigned to. It
still has to generate a new list object, which is then stored in the
appropriate part of the left-hand list object.

I suppose a sufficiently clever optimizing compiler might not do it
that way, but looking at some code with dis.dis() I'm pretty sure Python
does create the new list first.


-- 
Steven




More information about the Python-list mailing list