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

Goncalo Rodrigues op73418@mail.telepac.pt
Fri Jan 10 08:03:02 2003


----- Original Message -----
From: "antonmuhin на rambler.ru" <antonmuhin@rambler.ru>
To: <tutor@python.org>
Sent: Friday, January 10, 2003 10:16 AM
Subject: Re: [Tutor] how do you use __dict__ ? (Was: code dangerous?)


> Hello Alfred,
>
> I might be missing something, but why don't use simpler approach:
>
> class Person:
>       def __init__(self, name):
>           self.name = name
>           self.speakMethod = None
>
>       def speak(self):
>           if self.speakMethod:
>               self.speakMethod(self)
>           else:
>               print "%s says: my name is %s" % (self.name, self.name)"
>
>       def setSpeakMethod(self, speakMethod):
>           self.speakMethod = speakMethod
>
> def crankyspeak(person):
>     print "%s says: I don't tell anyone my name" % (person.name)
>
> hobbit = Person('Bilbo Baggins')
> cranky = Person('someone else')
>
> cranky.setSpeakMethod(crankyspeak)
>
> hobbit.speak()
> cranky.speak()
>

Ah, thanks. That was exactly what was lurking in the back of my mind!
Thanks for yanking it out!

> Best regards,
> Anton.
>

With my best regards,
G. Rodrigues