On Mon, Apr 25, 2022 at 03:38:21PM -0000, [email protected] wrote:
> Typically, when subclassing a NamedTuple type, you often don't want
> the <, >, <=, >=, + or * operators to work, so in that case you would
> want for the related methods to return NotImplemented.
When I have subclassed NamedTuple types, I have never done that.
If `obj` is a tuple, it supports those operators. If you subclass tuple,
and get a NamedTuple, the subclass is still a tuple, and the Liskov
Substitution Principle tells us that it should support all tuple
operations. If you subclass the NamedTuple, that is still a tuple, and
again Liskov tells us that it should behave like a tuple.
I'm the first person to acknowledge that Liskov is more of a guideline
than a law, so I won't say that what you are doing is *always* wrong,
but surely it is wrong more often than it is right.
In any case, your function is a one-line function. Not every one line
function needs to be a builtin. If you don't want to reimplement it each
and every time, it is easy enough to import it from your personal
utility library:
class Spam(MyNamedTuple):
from mytoolbox import NotImplementedMethod
__lt__ = __gt__ = NotImplementedMethod
Its not quite as convenient as a builtin, but on the plus side, you
don't have to write a PEP and then wait until Python 3.11 or 3.12 before
you can start using it.
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/YKMPFZDKSZNHXKPLW3422JX42HMFKSNY/
Code of Conduct: http://python.org/psf/codeofconduct/