471,342 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,342 software developers and data experts.

Passing messages between classes

bartonc
6,596 Expert 4TB
I'm working on a scheme which reduces the interface between classed to a single method and a list messages that the co-operative class knows. I invite all comments and/or critiques/improvements: We could even turn this into an article!
Expand|Select|Wrap|Line Numbers
  1. class AnyClass(object):
  2.     """Demonstrate massage passing:
  3.         This is the subordinate class."""
  4.     def Funct1(self, arg1):
  5.         print "AnyClass.Funct1 got", arg1
  6.         self.Send("MESSAGE3", arg1, string1='hello', string2='world')
  7.  
  8.     def Funct2(self, arg2, arg3):
  9.         print arg2, arg3
  10.  
  11.     def Funct3(self, count, **kwargs):
  12.         """Must document the names of the kwargs here:
  13.            Funct3(count, string1, string2)"""
  14.         print count, kwargs['string1'], kwargs['string2']
  15.  
  16.  
  17.     # Of cource, the sender and the reciver would typically have different massages
  18.     DispatchDict = {'MESSAGE1':Funct1, 'MESSAGE2':Funct2, 'MESSAGE3':Funct3}
  19.  
  20.     def Receive(self, *args, **kwargs):
  21.         self.DispatchDict[args[0]](self, *args[1:], **kwargs)
  22.  
  23.     def Connect(self, callback):
  24.         self.Send = callback
  25.         return self.Receive
  26.  
  27.  
  28. class AnyOtherClass(object):
  29.     """Demonstrate massage passing:
  30.         This is the superior class."""
  31.     def __init__(self):
  32.         self.Worker = AnyClass()
  33.         self.Send = self.Worker.Connect(self.Receive)
  34.         self.Send('MESSAGE1', 5)
  35.  
  36.     def Funct1(self, arg1):
  37.         print arg1
  38.  
  39.     def Funct2(self, arg2, arg3):
  40.         print arg2, arg3
  41.  
  42.     def Funct3(self, count, **kwargs):
  43.         """Must document the names of the kwargs here:
  44.            Funct3(count, string1, string2)"""
  45.         print count, kwargs['string1'], kwargs['string2']
  46.  
  47.  
  48.     # Of cource, the sender and the reciver would typically have different massages
  49.     DispatchDict = {'MESSAGE1':Funct1, 'MESSAGE2':Funct2, 'MESSAGE3':Funct3}
  50.  
  51.     def Receive(self, *args, **kwargs):
  52.         self.DispatchDict[args[0]](self, *args[1:], **kwargs)
  53.  
  54. test = AnyOtherClass()
  55.  
Nov 3 '07 #1
0 1246

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by cold | last post: by
2 posts views Thread by Gvs | last post: by
3 posts views Thread by Schorschi | last post: by
5 posts views Thread by Adam | last post: by
11 posts views Thread by Arsen Vladimirskiy | last post: by
3 posts views Thread by Curious | last post: by
10 posts views Thread by Stan | last post: by
12 posts views Thread by Andrew Bullock | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.