<div dir="ltr">On 4 February 2013 11:17, Tim Delaney <span dir="ltr"><<a href="mailto:timothy.c.delaney@gmail.com" target="_blank">timothy.c.delaney@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="im">On 4 February 2013 10:53, João Bernardo <span dir="ltr"><<a href="mailto:jbvsmo@gmail.com" target="_blank">jbvsmo@gmail.com</a>></span> wrote:<br>
</div><div class="gmail_extra"><div class="gmail_quote"><div class="im"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div dir="ltr"><div><div><div><div><div><div>Hi, about this enum/const thing, The use case I like more is a class where you know all the<br>instances and not just a sequence of names.<br></div>Particularly It would be nice to have custom attributes and methods besides the value and the name.<br>



<br></div>I have my own implementation with a basic api somewhat borrowed from flufl.enum (plus a lot of other stuff),<br>but with this kind of support: <a href="https://github.com/jbvsmo/makeobj" target="_blank">https://github.com/jbvsmo/makeobj</a></div>

</div></div></div></div></blockquote><div><br></div></div><div>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.</div>
</div></div></div></blockquote><div><br></div><div style>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.</div>
<div style><br></div><div style>Now you can do something like:</div><div style><br></div><div style><div><div>Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)] on win32</div><div>Type "help", "copyright", "credits" or "license" for more information.</div>
<div>>>> from enum import Enum, EnumValue, EnumMeta</div><div>>>></div><div>>>> class MyEnumValue1(EnumValue):</div><div>...     pass</div><div>...</div><div>>>> class MyEnumMeta1(EnumMeta):</div>
<div>...     @classmethod</div><div>...     def _create_value(cls, key, value):</div><div>...         return MyEnumValue1(key, value)</div><div>...</div><div>>>> class MyEnum1(Enum, metaclass=MyEnumMeta1):</div><div>
...     VALUE1,</div><div>...     VALUE2</div><div>...</div><div>>>> class MyEnumValue2(EnumValue):</div><div>...     pass</div><div>...</div><div>>>> class MyEnumMeta2(MyEnumMeta1):</div><div>...     @classmethod</div>
<div>...     def _create_value(cls, key, value):</div><div>...         return MyEnumValue2(key, value)</div><div>...</div><div>>>> class MyEnum2(MyEnum1, metaclass=MyEnumMeta2):</div><div>...     VALUE3,</div><div>
...     VALUE4</div><div>...</div><div>>>> print(repr(MyEnum1))</div><div><enum '__main__.MyEnum1' {<MyEnumValue1 'MyEnum1.VALUE1': 0>, <MyEnumValue1 'MyEnum1.VALUE2': 1>}></div>
<div>>>> print(repr(MyEnum2))</div><div><enum '__main__.MyEnum2' {<MyEnumValue1 'MyEnum1.VALUE1': 0>, <MyEnumValue1 'MyEnum1.VALUE2': 1>, <MyEnumValue2 'MyEnum2.VALUE3': 2>, <MyEnumValue2 'MyEnum2.VALUE4': 3>}></div>
<div>>>></div></div><div><div><br></div><div>Tim Delaney<br></div></div></div></div></div></div>