faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 1 people (100%) answered Yes

Entry

Enumerations (was: Parsing chemical formula)

Jul 5th, 2000 10:00
Nathan Wallace, Hans Nowak, Snippet 132, Christian Tismer


"""
Packages: miscellaneous.sick_and_twisted;basic_applications.enumeration
"""
"""
Christian Tismer wrote:
> 
> Sorry, tutor list,
> but I could not resist.
> I will stop this now. (Ahem)
Last refinement: To make an import of enum
possible, I have to obtain the globals from the lambda:
"""
# in somewhere.py
def enum(func):
    g = func.func_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
# Now I can do
# from somewhere import enum
enum(lambda ONE, TWO, THREE, FOUR, FIVE:1)
# added by PSST:
print ONE, TWO, THREE, FOUR, FIVE