[Tutor] Help with the technique "Menu Processing"

Andrew Wilkins toodles@yifan.net
Sun, 29 Apr 2001 21:05:54 +0800


Hi Juno,

What you need to do is set up the class so...

1) Each method's first argument is self. self refers to the instance itself,
if you don't know.

eg.
> 	def send_priv_msg(connection, nick, text):
> 		... send code ..
should be:
def send_priv_msg(self,connection,nick,text):
	blah blah blah

2) The msgtype dictionary's values should have self before the variables, to
tell python to look in the instance's own namespace.

eg.
> 		self.msgtype = {"priv":send_priv_msg,
should be:
self.msgtype={"priv":self.send_priv_msg, #and so on...

Now when you call them, it should be fine and dandy!

def send_msg_by(connection, nick, text, type):
> 		self.msgtype[type](connection, nick, text)

*looks puzzled*
You had it right in that one! =)

Hope you can understand my badly written attempt at helping...

Andrew

> -----Original Message-----
> From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
> juno@gamefire.com
> Sent: Sunday, 29 April 2001 4:31 PM
> To: tutor@python.org
> Subject: [Tutor] Help with the technique "Menu Processing"
>
>
> Hi Everyone,
> I was wondering if I might be able to get some help with "Menu
> Processing".
> What I'm looking for is an example of how it might be done within
> an object.
> I've been trying myself, but I keep getting an error.
>
> Here's an example of what I'm trying to do:
>
> Class msg:
> 	def __init__(self):
> 		self.msgtype = {"priv":send_priv_msg,
> 				"pub":send_pub_msg,
> 				"action":send_action_msg,
> 				"notice":send_notice_msg,
> 				"ctcp":send_ctcp_msg}
> 		...more stuff...
>
> 	def send_priv_msg(connection, nick, text):
> 		... send code ..
> 	def send_pub_msg(connection, nick, text):
> 		... send code ..
> 	def send_action_msg(connection, nick, text):
> 		... send code ..
> 	def send_notice_msg(connection, nick, text):
> 		... send code ..
> 	def send_ctcp_msg(connection, nick, text):
> 		... send code ..
>
> 	def send_msg_by(connection, nick, text, type):
> 		self.msgtype[type](connection, nick, text)
>
>
> Thanks,
>
> Juno
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>