[Python-Dev] feedback on Enum class (original) (raw)

Keith Dart kdart at kdart.com
Tue Dec 30 13:01:42 EST 2003


Greetings everyone. I hope you all had a great Christmas holiday. 8-)

I am posting this Enum (named number) class to this list for possible feedback. It can be used as a regular integer but when printed (stringified) it yeilds its name. The hash also makes named numbers with the same integer value into unique dictionary keys. That is the part I am not sure about... Comments appreciated.

class Enum(int): slots = ("_name") def new(cls, val, name): v = int.new(cls, val) v._name = str(name) return v def str(self): return self._name def repr(self): return "%s(%d, %r)" % (self.class.name, self,self._name) def hash(self): return int.hash(self) + hash(self._name)

-- -- ------------------------------------------------------------------------- Keith Dart <mailto:kdart at kdart.com> <http://www.kdart.com/>

Public key ID: B08B9D2C Public key: <http://www.kdart.com/~kdart/public.key>

-------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part Url : http://mail.python.org/pipermail/python-dev/attachments/20031230/df76d679/attachment.bin



More information about the Python-Dev mailing list