[Tutor] is this code dangerous?
Scott Widney
SWidney@ci.las-vegas.nv.us
Fri Jan 10 10:58:03 2003
> > class Person:
> > def __init__(self, name):
> > def speak(self, dummy):
> > def crankyspeak(self):
> >
> > hobbit = Person('Bilbo Baggins')
> > cranky = Person('someone else')
> > cranky.speak=Person.crankyspeak
Taking a turn down a different path: make the Person's 'mood' an attribute
and have the Person check his mood before speaking.
>>> class Person:
... def __init__(self, name, mood=None):
... self.name = name
... self.mood = mood
... def speak(self):
... if self.mood == 'cranky':
... print "We isn't saying nothing."
... else:
... print "Hello, I'm %s." % self.name
...
>>> bilbo = Person("Bilbo Baggins")
>>> gollum = Person("Smeagol", 'cranky')
>>> bilbo.speak()
Hello, I'm Bilbo Baggins.
>>> gollum.speak()
We isn't saying nothing.
>>> gollum.mood = 'happy'
>>> gollum.speak()
Hello, I'm Smeagol
>>>
Scott