a trick with lists ?

imho certo at comeno.it
Thu Feb 7 16:28:32 EST 2008


Steve Holden ha scritto:

>>> What I do not fully understand is the line "self.tasks[:] = tasks". 
>>> Why does the guy who coded this did not write it as "self.tasks = 
>>> tasks"? What is the use of the "[:]" trick ?
>>
>> It changes the list in-place. If it has been given to other objects, 
>> it might require that.
> 
> Nowadays it's stylistically better to write
> 
>     self.tasks = list(tasks)
> 
> as it does just the same and makes it a little clearer what's going on 
> (though of course if tasks *isn't* a list it won't do *exactly* the same.
> 
> regards
>  Steve

No:

self.tasks = list(tasks) is the same of self.tasks = tasks[:], not a 
replacement for self.tasks[:] = tasks , the latter performing a 
different operation, i.e. resetting the list self.tasks 'in place' 
without assigning it a different list.




More information about the Python-list mailing list