[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: a trick with lists ?

Matthew_WARREN

2/8/2008 11:25:00 AM




> On Feb 7, 12:20 pm, "Sébastien Vincent" <sebastien_nimp73<@>free.fr>
> wrote:
> > 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 ?
>
> if you do
> a = [1,2,3]
> b = []
> b = a
>
> then assign: b[1] = 9
> now a[1] == 9 as well
>
> with a[:] = b you are actually getting a copy of the list rather than
> an alias
>
> it's hard to say if this is needed in the case you described without
> context, but that's what the a[:] = b idiom does

Be wary of using a[:]=b when the list holds objects;

>>> a=[[1],[2],[3]]
>>> b[:]=a
>>> b
[[1], [2], [3]]
>>> b[0].append(4)
>>> b
[[1, 4], [2], [3]]
>>> a
[[1, 4], [2], [3]]
>>>

see reply to 'Brain stuck whats occurring here' by Gabriel for more info.

Matt
(do I really need to put an apology for the disclaimers. Like, a disclaimer
for the disclaimers???. if people stopped reposting them and complainin'
there would be a lot less of them.

However, you will be glad to hear today is my last day with Paribas, so
after today, there will be no more Paribas discalimers on my emails :)
--


This message and any attachments (the "message") is
intended solely for the addressees and is confidential.
If you receive this message in error, please delete it and
immediately notify the sender. Any use not in accord with
its purpose, any dissemination or disclosure, either whole
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message.
BNP PARIBAS (and its subsidiaries) shall (will) not
therefore be liable for the message if modified.
Do not print this message unless it is necessary,
consider the environment.

---------------------------------------------

Ce message et toutes les pieces jointes (ci-apres le
"message") sont etablis a l'intention exclusive de ses
destinataires et sont confidentiels. Si vous recevez ce
message par erreur, merci de le detruire et d'en avertir
immediatement l'expediteur. Toute utilisation de ce
message non conforme a sa destination, toute diffusion
ou toute publication, totale ou partielle, est interdite, sauf
autorisation expresse. L'internet ne permettant pas
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce
message, dans l'hypothese ou il aurait ete modifie.
N'imprimez ce message que si necessaire,
pensez a l'environnement.
1 Answer

Ed

3/20/2008 7:48:00 PM

0



David Lafayette Mapother wrote:

> Ed wrote:
> > Notice that he never once uses the word "a",
> > only "an" in front of consonants,
> > whatever that means.
>
> maybe he wants to become "an hero"
>
> If anyone doesn't know what "an hero" is,
> look it up at Encyclopedia Dramatica

More like an jerk, or an fool, or an loser, an troll.