Defining a Python enum in a C extension - am I doing this right?
Bartosz Golaszewski
brgl at bgdev.pl
Sat Jul 31 09:01:13 EDT 2021
On Fri, Jul 30, 2021 at 2:41 PM Serhiy Storchaka <storchaka at gmail.com> wrote:
>
> 23.07.21 11:20, Bartosz Golaszewski пише:
> > I'm working on a Python C extension and I would like to expose a
> > custom enum (as in: a class inheriting from enum.Enum) that would be
> > entirely defined in C.
>
> I think that it would be much easier to define it in Python, and then
> either import a Python module in your C code, or exec a Python code as a
> string.
>
You mean: evaluate a string like this:
'''
import enum
class FooBar(enum.Enum):
FOO = 1
BAR = 2
BAZ = 3
'''
And then pull in the FooBar type from the resulting dictionary into my
C code? Sounds good actually. I think I'll be able to add the FooBar
type to another type's tp_dict too - because some enums I want to
create will be nested in other classes.
Bart
More information about the Python-list
mailing list