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@kdart.com http://www.kdart.com/ ---------------------------------------------------------------------------- Public key ID: B08B9D2C Public key: http://www.kdart.com/~kdart/public.key ============================================================================
On Tue, Dec 30, 2003, Keith Dart wrote:
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.
This should probably go to comp.lang.python. -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ Weinberg's Second Law: If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization.
participants (2)
-
Aahz
-
Keith Dart