Steven Bethard wrote:

py> class enum(object): ... class item(object): ... def __init__(self, val): ... self.val = val ... def __repr__(self): ... return 'enum.item(%r)' % self.val ... def __init__(self, vals): ... self.items = [type(self).item(val) for val in vals] ... self._val_item_map = dict(zip(vals, self.items)) ... self._index_item_map = dict(enumerate(self.items))

Sorry, this was before I decided I should save items as an instance variable. Drop this map, it's unnecessary.


...     def __call__(self, val):
...         try:
...             return self._val_item_map[val]
...         except KeyError:
...             raise TypeError("%s is not a valid %s" % (val, type(self)))
...     def __getitem__(self, index):
...         return self._index_item_map[index]

And replace this with self.items[index]

STeVe
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to