Entry
Enumerations (was: parsing chemical formula)
Jul 5th, 2000 10:00
Nathan Wallace, Hans Nowak, Snippet 131, Christian Tismer
"""
Packages: basic_applications.enumeration;miscellaneous.sick_and_twisted
"""
"""
Sorry, tutor list,
but I could not resist.
I will stop this now. (Ahem)
Martijn Faassen wrote:
>
> Christian Tismer wrote:
>
> > Now, if you must, how 'bout this:
[second try. But watch out...]
> Hm, this is too many lines [whine, nag, never-pleased-ly yours]. As
> kludge trickery though, it's way cool!
>
> > I think I have to duck and cover if Timbot watches me :-)
>
> Indeed, Timbot is sure to catch us at this soon!
Now I will him give a final reason to pursue me.
"""
def enum(func):
g = globals() ; code = func.func_code
base = apply(func, (0,)*code.co_argcount) or 0
for i in range(code.co_argcount):
g[code.co_varnames[i]] = i+base
"""
Yeees, this is hefty.
But now watch out what I got for the application:
"""
enum(lambda ONE, TWO, THREE, FOUR, FIVE:1)
# this gives us an enumeration, based from 1. Believe me.
print ONE, TWO, THREE, FOUR, FIVE
# (1, 2, 3, 4, 5)
"""
Naaah? Oh yeah, now we really have a "useful" lambda example :-)))
ciao - chris.ducked.and.covered.for.the.rest.of....
"""