[Python-Dev] Concerns about method overriding and subclassing with dataclasses

Stephen J. Turnbull turnbull.stephen.fw at u.tsukuba.ac.jp
Sat Dec 30 10:27:22 EST 2017


Ethan Furman writes:

 > Good point.  So auto-generate a new __repr__ if:
 > 
 > - one is not provided, and
 > - existing __repr__ is either:
 >    - object.__repr__, or
 >    - a previous dataclass __repr__

-0.5 I'm with Guido here.  Just use the simple rule that a new
__repr__ is generated unless provided in the dataclass.

The logic I use (Guido seems to be just arguing for "simple" for now)
is that a dataclass is "usually" going to add fields, which you
"normally" want exposed in the repr, and that means that an
*inherited* __repr__ is going to be broken in some sense.  The code
author will disagree in "a few" cases, and in those cases they will
use repr=False to override.

I grant that there may be many reasons why one would be deriving
dataclasses from dataclasses without adding fields that should be in
the repr, so the quote marks above may be taken to be indications of
my lack of imagination. ;-)

Here's to 2018.  It *has* to be better than 2017 -- there will be a
Python feature release!

Steve



More information about the Python-Dev mailing list