Entry
Enum (was: Parsing chemical formula)
Jul 5th, 2000 10:00
Nathan Wallace, Hans Nowak, Snippet 108, Christian Tismer
"""
Packages: basic_applications.enumeration
"""
"""
[me, with an enum impl by strings]
> Cool, I hadn't thought of using string.split to reduce the amount of
> quotes (and commas!). Still not exactly of the elegance of:
>
> ONE, TWO, THREE = range(3) # this *looks* like Python code (as it is
> :)
>
> but almost there, and no counting!
Now, if you must, how 'bout this:
"""
def make_enum(func, base):
g = globals()
for i in range(func.func_code.co_argcount):
g[func.func_code.co_varnames[i]] = i+base
del g[func.__name__]
# after this, you can fake an enum like this
def enum(ONE, TWO, THREE) : pass
# and you activate it so:
make_enum(enum, 1)
# here we are:
print ONE, TWO, THREE
#(1, 2, 3)
"""
I think I have to duck and cover if Timbot watches me :-)
ciao - chris
"""