a trick with lists ?

Diez B. Roggisch deets at nospam.web.de
Thu Feb 7 18:32:13 CET 2008


"S����������������������������������������������" schrieb:
> I've found some class on the Net which takes basically this form :
> 
> ######
> class Foo:
>     def __init__(self):
>         self.tasks = []
>    ...
> 
>     def method1(self):
>         tasks = []
>         while True:
>   ...
>   append/pop elements into/from tasks
>   ...
>   if condition : break
> 
>     self.tasks[:] = tasks
>         return
> ######
> 
> 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.


Diez



More information about the Python-list mailing list