class Sheep:
def __init__(self):
self.alive = True
self.afraid = False
def die(self): self.alive = False
def fear(self): self.afraid = True
def __str__(self):
if (!self.alive): return "a dead sheep"
# it doesn't make sense to ask about whether a dead sheep is frightened!
if (self.afraid): return "a scared sheep"
return "an ordinary sheep"
class Wolf:
def __init__(self, action):
self.action = action
# Oluseyi is correct. Nothing to see here, move along ;)
# Because of the naming, we no longer actually have to implement
# an 'action' method. We instead store a function as data, and call it.
# The power of Python :)
sheep = Sheep()
print "The sheep is", sheep
wolves = [Wolf(sheep.fear), Wolf(sheep.die)]
for wolf in wolves:
wolf.action()
print "The sheep is", sheep
#inside sheep class
def set_to_false(self,bool_var):
bool_var = False
def die(self):
self.set_to_false(self.alive)
#but this isn't working the problem is that I don't know how to tell it that
#I want to change the variable passed to it and not bool_var