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

Nick Coghlan ncoghlan at gmail.com
Sun Nov 26 23:07:02 EST 2017


On 27 November 2017 at 06:22, Eric V. Smith <eric at trueblade.com> wrote:
> On 11/26/2017 1:04 PM, Brett Cannon wrote:
>>
>>     The only open issues I know of are:
>>     - Should object comparison require an exact match on the type?
>>     https://github.com/ericvsmith/dataclasses/issues/51
>>
>>
>> I say don't require the type comparison for duck typing purposes.
>
>
> The problem with that is that you end up with cases like this, which I don't
> think we want:
>
> @dataclass
> class Point:
>     x: int
>     y: int
>
> @dataclass
> class Point3d:
>     x: int
>     y: int
>     z: int
>
> assert Point(1, 2) == Point3d(1, 2, 3)

Perhaps the check could be:

  (type(lhs) == type(rhs) or fields(lhs) == fields(rhs)) and all
(individual fields match)

That way the exact type check would be an optimisation to speed up the
common case, while the formal semantic constraint would be that the
field definitions have to match (including their names and order).

Cheers,
Nick.

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


More information about the Python-Dev mailing list