[Python-Dev] PEP520 and absence of definition_order (original) (raw)

Ethan Furman ethan at stoneleaf.us
Sat Sep 10 03:49:43 EDT 2016


Per Victor's advice I'm posting this here.

PEP 520 has been accepted, but without the definition_order attribute. The accompanying comment:

"Note: Since compact dict has landed in 3.6, definitionorder has been removed. cls.dict now mostly accomplishes the same thing instead."

The "mostly" is what concerns me. Much like having a custom dir lets a class fine-tune what is of interest, a custom definition_order allows a class to present a unified view of the class creation process. This could be important to classes that employ getattr (or getattribute) to provide virtual attributes, such as Enum or proxy classes.

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.

For example, this class:

class PassBy(Enum): ... value = 1 ... reference = 2 ... name = 3 ... object = name ... def used_by_python(self): ... return self.name == 'name' ...

shows this:

PassBy.dict.keys() dict_keys([ 'generate_next_value', 'module', 'used_by_python', 'doc', 'member_names', 'member_map', 'member_type', 'value2member_map', 'reference', 'object', 'new', ])

Notice that two of the members are missing, and all are after the method.

If definition_order existed it would be this:

PassBy.definitionorder ['value', 'reference', 'name', 'object', 'used_by_python']

Which is a much more accurate picture of the user's class.

-- Ethan



More information about the Python-Dev mailing list