PEP 354: Enumerations in Python
Roy Smith
roy at panix.com
Mon Feb 27 21:12:51 EST 2006
A few random questions:
a = enum ('foo', 'bar', 'baz')
b = enum ('foo', 'bar', 'baz')
what's the value of the following:
a == b
a is b
a.foo == b.foo
a.foo is b.foo
len (a)
str (a)
repr (a)
hash (a)
type (a)
Can you make an enum from a sequence?
syllables = ['foo', 'bar', 'baz']
c = enum (syllables)
You imply that it works from "An enumerated type is created from a sequence
of arguments to the type's constructor", but I suspect that's not what you
intended.
BTW, I think this is a great proposal; enums are a badly needed part of the
language. There's been a number of threads recently where people called
regex methods with flags (i.e. re.I) when integers were expected, with
bizarre results. Making the flags into an enum would solve the problem
while retaining backwards compatibility. You would just have to put in the
re module something like:
flags = enum ('I', 'L', 'M', 'S', 'U', 'X')
I = flags.I
L = flags.L
M = flags.M
S = flags.S
U = flags.U
X = flags.X
then the methods which expect a flag can do:
if flag is not in re.flags:
raise ValueError ("not a valid flag")
and the ones which expect an integer would (if nothing better), raise
NotImplemented when they tried to use the value as an integer if you passed
it a flag by mistake.
More information about the Python-list
mailing list