[Python-Dev] Second post: PEP 557, Data Classes

Nick Coghlan ncoghlan at gmail.com
Wed Nov 29 01:19:50 EST 2017


On 29 November 2017 at 04:31, Eric V. Smith <eric at trueblade.com> wrote:
> On 11/28/17 7:02 AM, Nick Coghlan wrote:
>> So in the above example, you would have:
>>
>>    >>> B.__field_layout__ is B
>>    True
>>    >>> C1.__field_layout__ is B
>>    True
>>    >>> C2.__field_layout__ is B
>>    True
>>
>> It would then be up to the dataclass decorator to set
>> `__field_layout__` correctly, using the follow rules:
>>
>> 1. Use the just-defined class if the class defines any fields
>> 2. Use the just-defined class if it inherits from multiple base
>> classes that define fields and don't already share an MRO
>> 3. Use a base class if that's either the only base class that defines
>> fields, or if all other base classes that define fields are already in
>> the MRO of that base class
>
>
> That seems like a lot of complication for a feature that will be rarely
> used. I'll give it some thought, especially the MI logic.
>
> I think what you're laying out is an optimization for "do the classes have
> identical fields, inherited through a common base class or classes", right?

It's a combination of that and "How do I get my own class to compare
equal with a dataclass instance?".

However, having the dataclass methods return NotImplemented for
mismatched types should be enough to enable interoperability, since it
will leave the question up to the other type.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list