[Python-Dev] feedback on Enum class

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>
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