[Tutor] remove instances from a list

John Fouhy john at fouhy.net
Mon Sep 17 02:43:54 CEST 2007


On 17/09/2007, Ara Kooser <ghashsnaga at gmail.com> wrote:
> 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.
[...]
> class Yeast:
[...]
>     def isAlive(self):
>         if self.life > 0:
>             self.alive = True
>         else:
>             self.alive = False

Most programmers would see an 'isAlive' method as a method that would
return True if the thing is alive, and False otherwise.

So you would define it as:

    def isAlive(self):
        if self.life > 0:
            return True
        else:
            return False

Then you could write code like this:

# suppose 'y' is a yeast object

# If y is alive, make it grow.
if y.isAlive():
    y.grow()

(actually, you can write this method much more concisely:

    def isAlive(self):
        return self.life > 0
)

HTH!

-- 
John.


More information about the Tutor mailing list