Need an enum type extension
sue
smalleys at gte.net
Fri Apr 14 00:05:27 EDT 2000
Will, that idiom does generate integers.
Frederic, I need some of the tuple/list functionality as
well. I'm not sure what your code does. I haven't done
lambdas except for number crunching.
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.
Strings don't work directly because I need _some_ of the
list/tuple functionality.
I started with the mess below, but it can't put the EnumLits
in the current scope. And I could see it getting ugly real
fast.
class EnumLit:
count = 0
def __init__( self, name ):
self.name = name
self.value = self.count
self.repr = self.count
self.count = self.count + 1
self.lock = 0
def set_repr( self, repr ):
if not self.lock: self.repr = repr
def finalize( self ):
self.lock = 1
self.count = 0
def index( self ): return self.index # etc
class Enum:
def __init__( self, *args ):
self.lock = 0
self.names = args
self.data = map( EnumLit, args )
# or dict, or DictList?
def set_repr( self, **args ):
for (name, val) in args:
if name in self.args:
i = self.names.index(name)
e = self.data[i]
e.set_repr( val )
self.finalize()
def finalize( self ):
self.lock = 1
and maybe some get/setattr stuff to finalize on any other
kind of access?
Sue.
More information about the Python-list
mailing list