[Python-ideas] PEP for enum library type?

Yuval Greenfield ubershmekel at gmail.com
Tue Feb 12 21:38:21 CET 2013


On Tue, Feb 12, 2013 at 9:58 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

> 1)  Magic is fun.  :)
>

I heartily agree. Check this out:

>>> RED, GREEN, BLUE = enum()
>>> print(RED, GREEN, BLUE)
0 1 2
>>> LEFT, TOP, RIGHT, DOWN = enum()
>>> print(LEFT, TOP, RIGHT, DOWN)
0 1 2 3


And here's the dirty trick....


import inspect
def enum():
    """If you use this  your computer will most likely burst ablaze and
your teeth will fall off"""
    frame = inspect.stack()[1][0]
    # 4 = 3 for the function call "enum()", 1 for the opcode
"UNPACK_SEQUENCE",
    # the next two bytes represent how many things we need to unpack.
    a, b = frame.f_code.co_code[frame.f_lasti + 4:frame.f_lasti + 6]
    enum_len = a + b * 256
    return range(enum_len)




Cheers,


Yuval
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130212/a78cbdaf/attachment.html>


More information about the Python-ideas mailing list