Need an enum type extension

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Fri Apr 14 11:30:04 CEST 2000


sue wrote in comp.lang.python:
> In Ada, I can do things like:
> 
> type MyEnum is ( A, B, C );
> for MyEnum use ( A : 3; B : 7; C : 13 );
> 
> Ada uses "'string" to refer to attributes.
> 
> MyEnum'len == 3.	(number of items in list)
> A'next == B		(next item in list)
> A'value == 3		(its low-level representation)
> B'index = 1		(its position in the list)
> 
> But A == 3 fails.  only A == A passes.  A'value == 3 is
> true.

This simple try seems to do that, at least. It needs some work (EnumVar
needs to be immutable, I suppose), but maybe you can base it on this?

class EnumVar:
  def __init__(self, value, index, enumlist):
    self.value = value
    self.index = index
    self.__enumlist = enumlist
  def next(self):
    return self.__enumlist.get_enum(self.index+1)
	
class EnumList:
  def __init__(self, values):
    self.__list = []
	self.len = len(values)
    for i in range(self.len):
      self.__list.append(EnumVar(values[i], i, self)
  def get_enum(self, i):
    return self.__list[i]
  def get_list(self):
    return self.__list
	
Now use this like:

>>> MyEnum = EnumList([3, 7, 13])
>>> A, B, C = MyEnum.get_list()
>>> MyEnum.len
3
>>> A.next() is B
1
>>> A.value
3
>>> B.index
1
>>> A == 3
0
>>> A == A
1



-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
 12:26pm  up 38 days, 23:46,  8 users,  load average: 1.49, 1.23, 1.18



More information about the Python-list mailing list