Problems with subclassing enum34

88888 Dihedral dihedral88888 at gmail.com
Fri Jun 28 07:52:54 EDT 2013


Thomas Heller於 2013年6月28日星期五UTC+8下午6時48分38秒寫道:
> trying out the enum34 module.
> 
> 
> 
> What I want to create is a subclass of enum.Enum that is also
> 
> based on ctypes.c_int so that I can better use enum instances
> 
> in ctypes api calls.
> 
> 
> 
> When I do this, I get a metaclass conflict:
> 
> 
> 
> 
> 
>  >>> class MyEnum(ctypes.c_int, enum.Enum):
> 
> ...    FOOBAR = 0
> 
> ...
> 
> Traceback (most recent call last):
> 
>    File "<stdin>", line 1, in <module>
> 
> TypeError: Error when calling the metaclass bases
> 
>      metaclass conflict: the metaclass of a derived class must be a 
> 
> (non-strict) subclass of the metaclasses of all its bases
> 
>  >>>
> 
> 
> 
> 
> 
> 
> 
> When I do this, it does not work either:
> 
> 
> 
>  >>> class MyEnum_meta(type(ctypes.c_int), type(enum.Enum)):
> 
> ...     pass
> 
> ...
> 
>  >>> class MyEnum(ctypes.c_int, enum.Enum):
> 
> ...     FOOBAR = 42
> 
> ...     __metaclass__ = MyEnum_meta
> 
> ...
> 
>  >>> MyEnum.FOOBAR
> 
> 42
> 
>  >>>
> 
> 
> 
> It should have printed '<MyEnum.FOOBAR: 42>'.
> 
> 
> 
> Any ideas?
> 
> 
> 
> Thanks,
> 
> Thomas

Just use a dictionary for the job. Python i not c/c++.



More information about the Python-list mailing list