Is this..... evil?

Philip Swartzleonard starx at pacbell.net
Sat Feb 9 22:23:03 EST 2002


Quinn Dunkan || Sat 09 Feb 2002 06:41:41p:

> On 9 Feb 2002 18:21:31 GMT, Philip Swartzleonard <starx at pacbell.net>
> wrote: 
>> [Origional]
 
> I recommend leaving the 'type' attributes out.
> 
> class Message: pass
> class Fighto(Message): pass
> class Spello(Message): pass
> 
> class Receiver: # i before e except after c!
>     def message(self, msg):
>         msgname = 'rec_' + msg.__class__.__name__
>         if hasattr(self, msgno):
>             # recommend leaving out the callable(), why let people
>             define # non callable rec_ attrs?
>             getattr(self, msgname)(message)

Oh, so that's how you do that. I wrote this only using what knowledge i had 
available on hand... that's what i wanted, but didn't know how at the time, 
i think :)

Oh, and i laredy gave up on the 'callable' thingie, and stuck something 
like this in the comments:

Bla bla bla... Do not name any attributes of subs of this class anything 
beginning with "rec_" (except functions that take one 'message' paramater)
or you may be burned. Thank you.

 
> To the other poster who had the idea of using dicts to alias messages,
> you can do the same with:
> 
> class F(Receiver):
>     def rec_fighto(self, message):
>         print "FIGHTO!!"
>     rec_hacko = rec_fighto

You have gained a point in understanding python. Only a thousand more!
That's a really nice way of doing something like that, i think :)

>>Also, is there any real reason to have the message classes inherit when
>>the only thing that seems to identify them is an attribute? 
> 
> No, unless you wanted messages to have some messages of their own,
> which I assume you did since otherwise you could have made them
> strings.  If you do want all Messages to respond to a few common
> messages (small 'm'), like 'sender' or 'time_sent', then inheritance is
> natural. 

Well, i definatly wanted to attach data to them... and just about 
everything has a sender, i think. I haden't thought of that and inheritance 
at the same time i guess :)



-- 
Philip Sw "Starweaver" [rasx] :: www.rubydragon.com



More information about the Python-list mailing list