[Python-ideas] constant/enum type in stdlib

Eli Bendersky eliben at gmail.com
Mon Feb 11 14:28:45 CET 2013


On Sun, Feb 10, 2013 at 2:43 PM, Tim Delaney <timothy.c.delaney at gmail.com>wrote:

> On 4 February 2013 11:17, Tim Delaney <timothy.c.delaney at gmail.com> wrote:
>
>> On 4 February 2013 10:53, João Bernardo <jbvsmo at gmail.com> wrote:
>>
>>> Hi, about this enum/const thing, The use case I like more is a class
>>> where you know all the
>>> instances and not just a sequence of names.
>>> Particularly It would be nice to have custom attributes and methods
>>> besides the value and the name.
>>>
>>> I have my own implementation with a basic api somewhat borrowed from
>>> flufl.enum (plus a lot of other stuff),
>>> but with this kind of support: https://github.com/jbvsmo/makeobj
>>>
>>
>> I considered it, and in fact you could almost do it with my
>> implementation by using a custom subclass of EnumValue (except trying it
>> has just exposed a design flaw with the whole _EnumProxy bit). Works if you
>> create the enum in the same module as EnumValues, fails otherwise. Going to
>> have to have a rethink.
>>
>
> Fixed the _EnumProxy issue (but it's a kludge - I've used sys._getframe()
> - there's probably a better way). I've also made it so that you can
> override the metaclass to return a subclass of EnumValue.
>
> Now you can do something like:
>
> Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64
> bit (AMD64)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from enum import Enum, EnumValue, EnumMeta
> >>>
> >>> class MyEnumValue1(EnumValue):
> ...     pass
> ...
> >>> class MyEnumMeta1(EnumMeta):
> ...     @classmethod
> ...     def _create_value(cls, key, value):
> ...         return MyEnumValue1(key, value)
> ...
> >>> class MyEnum1(Enum, metaclass=MyEnumMeta1):
>  ...     VALUE1,
> ...     VALUE2
> ...
> >>> class MyEnumValue2(EnumValue):
> ...     pass
> ...
> >>> class MyEnumMeta2(MyEnumMeta1):
> ...     @classmethod
> ...     def _create_value(cls, key, value):
> ...         return MyEnumValue2(key, value)
> ...
> >>> class MyEnum2(MyEnum1, metaclass=MyEnumMeta2):
> ...     VALUE3,
> ...     VALUE4
> ...
> >>> print(repr(MyEnum1))
> <enum '__main__.MyEnum1' {<MyEnumValue1 'MyEnum1.VALUE1': 0>,
> <MyEnumValue1 'MyEnum1.VALUE2': 1>}>
> >>> print(repr(MyEnum2))
> <enum '__main__.MyEnum2' {<MyEnumValue1 'MyEnum1.VALUE1': 0>,
> <MyEnumValue1 'MyEnum1.VALUE2': 1>, <MyEnumValue2 'MyEnum2.VALUE3': 2>,
> <MyEnumValue2 'MyEnum2.VALUE4': 3>}>
> >>>
>

Can you elaborate on the utility of this feature? What realistic use cases
do you see for it? I think that at this point it's important to weigh all
benefits of features vs. implementation complexity, and there's absolutely
no need to support every feature every other enum implementation has. I
want to stress again that the most important characteristic of your
implementation is the clean syntax which means that enums are so easy to
define they don't really need special Python syntax and a library feature
can do. However, there's a big leap from this to defining custom
metaclasses for enums.

Eli
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130211/27f5899c/attachment.html>


More information about the Python-ideas mailing list