[Tutor] how do you use __dict__ ? (Was: code dangerous?)

Alfred Milgrom fredm@smartypantsco.com
Fri Jan 10 09:16:35 2003


Thanks you all for helping me out, and explaining multiple inheritance,=20
__dict__, and so on.

I hope I am not boring everyone, but I kept on fiddling with the code, and=
=20
the following seems to be a simpler way to override the 'speak' method, and=
=20
seems to work:

********************************************************
class Person:
      def __init__(self, name):
          self.name =3D name

      def speak(self):
          print "%s says: my name is %s" % (self.name, self.name)

      def crankyspeak(self):
          print "%s says: I don't tell anyone my name" % (self.name)

hobbit =3D Person('Bilbo Baggins')

cranky =3D Person('someone else')
cranky.speak=3Dcranky.crankyspeak

hobbit.speak()
cranky.speak()


********************************************************
This appears to overcome the issues raised about other approaches, and the=
=20
problem mentioned in Gon=E7alo Rodrigues' earlier posting about the=20
difference between a method and a plain function object.

Please do let me know if I am missing something.

Thanks,
Fred Milgrom=20