[Tutor] >> BANANAS! << Removing a class instance from a list?
Harry Kattz
gibbs05@flash.net
Tue, 6 Mar 2001 09:28:44 -0600
Greetings Chris,
I'm afraid you'll have to do this the old fashioned way. :-) Loop through
looking for the object and then remove it. Since you're storing the banana
name that would be a good key.
For example.
def RemoveBanana(Bananas, BananaName):
for CurrentBanana in Bananas:
if CurrentBanana.banana_name == BananaName:
Bananas.remove(CurrentBanana)
Return
That should work.
Good luck,
Sam
> Hey folx,
> Got a weird one here. I have a little program to
> grow some bananas. I have a banana class. It has
> methods for maturing at specified intervals,
> displaying different bitmaps at different life stages,
> and spawning new plants at maturity.
> The program works by creating a list of the class
> instances:
>
> bananacount = input('How many bananas do you wanna
> see? ')
> for x in range(0, bananacount):
> banananame = "banana" + str(x)
> new_banana = flora.Banana(banananame, 0, 0)
> bananas.append(new_banana)
>
> The code above creates the original list. Then when
> bananas get propagated, we append to the list:
>
> def bananaMaker(baby_x, baby_y, banana_name, bananas):
> new_banana = Banana(banana_name, baby_x, baby_y)
> bananas.append(new_banana)
>
> So far, so good, right? It's easy to append to the
> list. The problem comes when I try to remove items
> from the list. I'm supposed to use the value of the
> item I want to remove. But what is the value of a
> class instance? If I create two bananas and then
> print my list, I get something like this:
>
> [<flora.banana instance at 0081168C>, <flora.banana
> instance at 0081144C>]
>
> How do I feed that into banana.remove(listitemvalue)?
>
> Thanks in advance for your help. We're being overrun
> here.
>
> - Chris
>
> PS - On the other hand, it's pretty cool to be able to
> say that I've crashed my computer with bananas. :-)
>
> __________________________________________________
> Do You Yahoo!?
> Get email at your own domain with Yahoo! Mail.
> http://personal.mail.yahoo.com/
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor