[Python-Dev] PEP520 and absence of __definition_order__

Ethan Furman ethan at stoneleaf.us
Sun Sep 11 15:12:48 EDT 2016


On 09/11/2016 01:55 AM, Victor Stinner wrote:
> 2016-09-10 3:49 GMT-04:00 Ethan Furman wrote:

>> With __definition_order__ Enum can display the actual creation order of enum
>> members and methods, while relying on Enum.__dict__.keys() presents a
>> jumbled mess with many attributes the user never wrote, the enum members either
>> appearing /after/ all the methods (even if actually written before), or
>> entirely absent.
>
> Python 3.5 also returns methods in Enum.__dict__(). So it would be a
> new feature, right?

__definition_order__ is (would be) a new feature, yes.

> The use case seems to be specific to Enum. Can't you add a new method
> which only returns members (ordered by insertion order)?

The use case is specific to any custom metaclass that does more than enhance the attributes and/or methods already in the class body.

> list(myenum._member_maps.keys()) returns members, sorted by insertion
> order. Is it what you want?

That only includes members, not other attributes nor methods.  What I want is to make sure the other points of PEP 520 are not forgotten about, and that Enum conforms to the accepted PEP.

> Code:
> ---
> import enum
>
> class Color(enum.Enum):
>      red = 1
>      blue = red
>      green = 2
>
> print(Color.__dict__.keys())
> print(list(Color._member_map_.keys()))
> ---
>
> Python 3.5:
> ---
> dict_keys(['__module__', '_member_names_', 'green', '_member_type_',
> 'blue', '_value2member_map_', '_member_map_', '__new__', 'red',
> '__doc__'])
> ['red', 'blue', 'green']
> ---
>
> Python 3.6:
> ---
> dict_keys(['_generate_next_value_', '__module__', '__doc__',
> '_member_names_', '_member_map_', '_member_type_',
> '_value2member_map_', 'red', 'blue', 'green', '__new__'])
> ['red', 'blue', 'green']
> ---
>
> Note: It seems like dir(myenum) ignores "aliases" like blue=red in my example.

That is intentional.

--
~Ethan~


More information about the Python-Dev mailing list