[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Concerning copy
- To: pygame-users@xxxxxxxx
- Subject: Re: [pygame] Concerning copy
- From: "Patrick Mullen" <saluk64007@xxxxxxxxx>
- Date: Wed, 26 Apr 2006 12:37:04 -0700
- Delivered-to: archiver@seul.org
- Delivered-to: pygame-users-outgoing@seul.org
- Delivered-to: pygame-users@seul.org
- Delivery-date: Wed, 26 Apr 2006 15:37:09 -0400
- Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:references; b=kEXHjQhFy3kHGEICQNBeeL/HkTUExKGM3KeJ3uZ5aq0AjytPF44Kyl7KGukVI2bgFgl9pesqHBtdKU5fBPqesaNTBfsk3e10KloyYHNUE9Fiu4bfkdrtzlnVUNHZovfghPJTB8dVQlNAS9JTgS83hvuxV+bNdiX4Ya1dbgO7iWs=
- In-reply-to: <444DEBB6.70706@xepher.net>
- References: <444DEBB6.70706@xepher.net>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
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)