Is this..... evil?
Philip Swartzleonard
starx at pacbell.net
Sat Feb 9 13:21:31 EST 2002
So i've been poking around with ways of doing this messaging thing, and
i think up this (sorry for the inanity of some of the names, i was sick
and tired when i worte this :):
class Message:
type = "none"
class FightoMessage(Message):
type = "fighto"
class SpelloMessage(Message):
type = "spello"
class Reciever:
def message( self, message ):
methname = 'rec_' + message.type
if hasattr(self, methname):
if callable(getattr(self, methname)):
getattr(self, methname)(message)
class F(Reciever):
def rec_fighto(self, message):
print "FIGHTO!!"
class S(Reciever):
def rec_spello(self, message):
print "SPELLO!!"
def rec_fighto(self, message):
print "Damnit i'm a wizard not a whacker!"
def rec_none(self, message):
print "Er, what?"
I dont think a better solution i could ever have seen. It does all the
work of a big ugly switch statment, with none of the uglyness OR the
hard-codedness. I can define new message types at will and only update
things that i WANT to recieve them.
Then i realized it would have taken me three days to do this in C++,
compared to the 15 or so in python, and the former solution would have
been some kind of ugly hack involving rtti and what have you. I think
python may have finally risen to the complete and utter top in my mind
(O)_(O).
Anyway, i was just wondering, since this seems so great, is there
anything wrong with doing things this way that i'm not seeing?
Also, is there any real reason to have the message classes inherit when
the only thing that seems to identify them is an attribute?
Thanks %)
--
Philip Sw "Starweaver" [rasx] :: www.rubydragon.com
More information about the Python-list
mailing list