[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] lists of tuples



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