Antony Lee <anntzer....@gmail.com> added the comment:

To be honest, I don't really remember what exact use case I had in my mind 2 
years ago (as I probably worked around it in one way or another).  However, one 
example that I can think of (and that I have actually implemented before) is 
auto-conversion of C #defines from a C header file to a Python-level enum (e.g. 
for semi-automatic generation of a ctypes wrapper):

    # A general header parser (untested, just an example)
    def parse_defines(header_file):
        d = {}
        for line in header_file:
            if line.startswith("#define"):
                _, k, v = line.split()
                d[k] = int(v)
        return d

    # Now wrapping a specific C library
    foo_defines = parse_defines("foo.h")

    class Foo(Enum):
        locals().update({k: v for k, v in foo_defines.items() if 
k.startswith("FOO_")})

        def some_method(self):
            ...
            # e.g. call a C function that takes a FOO_* as parameter.

Obviously I could always just replace the method by a free function, but that's 
true for (nearly) all methods.  In other words, it seems a bit "unfair" that it 
is easy to define methods on enums where all options are explicitly listed, but 
very hard(?) to do so on enums with programatically defined options.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue34750>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to