[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] lists of tuples
- To: pygame-users@xxxxxxxx
- Subject: Re: [pygame] lists of tuples
- From: Sami Hangaslammi <sami.hangaslammi@xxxxxxxxx>
- Date: Sat, 10 Sep 2005 12:01:26 +0300
- Delivered-to: archiver@seul.org
- Delivered-to: pygame-users-outgoing@seul.org
- Delivered-to: pygame-users@seul.org
- Delivery-date: Sat, 10 Sep 2005 05:01:39 -0400
- Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=JYKj4iOrAJXuTl00odDVKA+miMs3XFveDDeNioN7lJerWOrUNEJaTlSzVuj1g9aFtge3jQL9lVSyXxI0Ap1k7wv8sOU30sy4+OD1aczQ//UZZfkY2eNe9yKed2AXdfZUssWBLdyH/8iagI2CTXLsGLsyrhf2wkvJFsMfAtJ9tFA=
- In-reply-to: <e823077805091000317cb39c16@mail.gmail.com>
- References: <e823077805091000317cb39c16@mail.gmail.com>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
On 9/10/05, Luke Miller <dodgyville@xxxxxxxxx> wrote:
> My question:
>
> I have a list of tuples that contains references to data inside my
> sprite class (in this case, their positions), I would like to edit the
> data in the tuple and have it change the data in the sprite, but this
> is proving difficult with immutable tuples.
>
> An example of my list would be:
> myList = [(sprite1.position, "text"), (sprite2.position, "outpt")]
>
> I would like to do an operation something like:
> myList[0][0] = (5,5) #I know this is wrong for so many reasons
>
> and have the the sprite1.position set to (5,5).
Actually, this has nothing to do with the immutability of tuples.Even
if you do myList = [[sprite1.position, "text"], [sprite2.position,
"output"]] you can then assign
myList[0][0] = (5,5), which will give you myList == [[(5,5), "text"],
[sprite2.position, "output"]]
So basically, assigning to a list will never touch the objects
referenced by that list.This is a very sane rule and preferrable most
of the time.
Alternative implementation could be:
myList = [(sprite1, "text"), (sprite2, "output")]
Then you can assign myList[0].position = (5,5)
--
Sami Hangaslammi