= Subclassing Dictionaries = The process differs by Python version. == Python-2.2 == Derive from {{{dict}}}. ex: {{{ #!python class Msg(dict): __slots__ = [] # no attributes def __init__(self, msg_type, kv_dict = {}): dict.__init__(self) self["msg-type"] = msg_type self.update(kv_dict) def Type(self): return self["msg-type"] def __getitem__(self, k): return self.get(k, None) def __delitem__(self, k): if self.has_key(k): dict.__delitem__(self, k) def __str__(self): pp = pprint.pformat(dict(self)) return "%s: %s" % (self.Type(), pp) }}} The {{{__slots__}}} line indicates that Msg has no attributes of its own, preserving memory; see UsingSlots. == See Also == [[Python-2.2]], SubclassingBuiltInTypes, UsingSlots == Questions == * Is this bad Python-2.2 code? Make improvements..! I ''do'' think it's worth showing how to use slots in the context of subclassing dict; In many cases, I think, people would want to do it. I ''do'' wonder if slots should be specified before or after the initializer- something to put on the UsingSlots page. -- LionKimbro <> [[lwickjr]]: Before, I think, collected with the other declaritives.