[Tutor] remove instances from a list

Ian Witham witham.ian at gmail.com
Mon Sep 17 02:59:53 CEST 2007


On 9/17/07, Ara Kooser <ghashsnaga at gmail.com> wrote:
>
> Hello all,
>
>    On my continuing quest to grapple with OO programming Kent showed
> me that I could call instances and store them in a list like:
> yeasts =
> [Yeast("Red_1","Red","ade","lys"),Yeast("Yellow_1","Yellow","lys","ade"),
>
>           Yeast("Red_2","Red","ade","lys"),Yeast("Yellow_2","Yellow","lys","ade")]
>
> Give certain conditions I want the yeast cell to die. Kent suggested I
> use something this:
> yeasts = [yeast for yeast in yeasts if yeast.isAlive()] to clear out
> dead yeast.
>
> Is the translation for the above line of code into pseudocode?
> yeast for every yeast in the list yeasts if the yeast method returned
> isAlive()


...yeast for yeast in yeasts if the yeast.isAlive() method returns True OR a
value that evaluates as True.

for instance 0 is false, any other integer is True,
0.0 is False, any other float is True
"" is False, and any non empty string is True.
Each data type has a True or False condition, which I believe is why Python
didn't even have a boolean True/False data type until very recently.

Or is this meant to be a true or false return? I have included the
> code below. Thank you again.


It does not need to return True or False, as whatever it returns can be
evaluated as True or False, as I mentioned above.
You should also realise that you can test for a specific return value by
doing something like this:

yeasts = [yeast for yeast in yeasts if yeast.isAlive() == "I'm not dead
yet!"]

or

yeasts = [yeast for yeast in yeasts if yeast.health > -10]
(this example directly tests the instance's local variable life_force rather
than calling a method and testing the returned value)

Hope this helps,

Ian.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070917/db47a5f7/attachment.htm 


More information about the Tutor mailing list