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

Re: [pygame] Concerning copy



A nice way to copy a list and its contents would be [copy.copy(o) for o in a_list].

If you only need to copy a list and not its contents (list of strings, ints), you can use the shortcut with a slice: a_list[:]

I rarely use copy myself, and when I do need a full copy I usually don't deal with the copy module and just write a clone() method myself.  That way I know exactly what's going on inside my class.  Or maybe I just like rewritting things needlessly :)  Of course even if you use the copy module you kind of need to write a __copy__ method anyway so...

I am curious what the original poster needed to copy, so far in pygame the only thing I need to make sure are copied are when I copy positions between objects, which did throw me off at first (how come everything is being displayed at the same point!).

Now I just make sure that when I initialize say, a bullet on my ships gun, I do this:

bullet = Bullet(ship.pos[:])

instead of:

bullet = Bullet(ship.pos)