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

Raymond Hettinger raymond.hettinger at gmail.com
Sat Dec 30 06:20:39 EST 2017


> On Dec 29, 2017, at 4:52 PM, Guido van Rossum <guido at python.org> wrote:
> 
> I still think it should overrides anything that's just inherited but nothing that's defined in the class being decorated.

This has the virtue of being easy to explain, and it will help with debugging by honoring the code proximate to the decorator :-)

For what it is worth, the functools.total_ordering class decorator does something similar -- though not exactly the same.  A root comparison method is considered user-specified if it is different than the default method provided by object: 

    def total_ordering(cls):
        """Class decorator that fills in missing ordering methods"""
        # Find user-defined comparisons (not those inherited from object).
        roots = {op for op in _convert if getattr(cls, op, None) is not getattr(object, op, None)}
        ...

The @dataclass decorator has a much broader mandate and we have almost no experience with it, so it is hard to know what legitimate use cases will arise.


Raymond



More information about the Python-Dev mailing list