I have a relatively large enum wherein each member represents a message type. A client will receive a message containing the integer value associated with the msg type in the enum. For each msg type there will be an individual function callback to handle the msg.
I'd like to make the lookup and dispatching of the callback as quick as possible by using a sparse array (or vector) in which the enum value maps to the index of the callback. Is this possible in Python given arrays can't hold function types?
#pseudo code for 'enum'
class MsgType(object):
LOGIN, LOGOUT, HEARTBEAT, ... = range(n)
#handler class
class Handler(object):
def handleMsg(self, msg):
#dispatch msg to specific handler
def __onLogin(self, msg):
#handle login
def __onLogout(self, msg):
#handle logout
Update: I wasn't clear in my terminology. I now understand Python dictionary lookups to be of complexity O(1) which makes them the perfect candidate. Thanks.
dict
. – David Heffernan Jun 13 '12 at 19:27dict
to solve this problem. – steveha Jun 13 '12 at 19:28