This is somewhere that I would personally use a metaclass. That way, if you define more subclasses of Message, you're not limited to doing so in that single module.
Someone correct me if this is goofy, I don't do much metaclass programming. Perhaps something like: #!/usr/bin/python class MetaMessage(type): nmap = {} def __new__(mcl, name, bases, d): obj = super(MetaMessage, mcl).__new__(mcl, name, bases, d) # Check __name__ here as 'Message' isn't yet defined. if "Message" in [b.__name__ for b in bases]: if "number" not in d: raise TypeError("Message protocol expects a number attribute") MetaMessage.nmap[d['number']] = obj # Complete Creation. return obj class Message(object): __metaclass__ = MetaMessage class MessageOne(Message): number = 1 class MessageTwo(Message): number = 2 class MessageFourThousandThreeHundredAndTwentyTwoPointOne(Message): number = 4322.1 print MetaMessage.nmap Which results in: mac:~ jeff$ !p python test.py {1: <class '__main__.MessageOne'>, 2: <class '__main__.MessageTwo'>, 4322.1000000000004: <class '__main__.MessageFourThousandThreeHundredAndTwentyTwoPointOne'>} mac:~ jeff$ Thanks! Jeff On 3/19/08, benhoyt <[EMAIL PROTECTED]> wrote: > > Replying to myself here, after discovering more. :-) > > > > Is there a way to get __thismodule__ in Python? > > > It looks like __thismodule__ is just sys.modules[__name__]. Neat. > > Hmmm ... does sys.modules always already contain the currently-being- > loaded module? Or is this a hack that only happens to work? (It does; > I've tested it now.) Just wondering, because the Python docs say that > sys.modules is "a dictionary that maps module names to modules which > have *already been loaded*." > > > > if isinstance(attr, Message): > > nmap[attr.number] = attr > > > Oops, this was untested code. I actually meant issubclass (or > something similar) here, not isinstance. > > > Cheers, > Ben. > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list