NotImplementedMethod function, or making NotImplemented return itself when called

This is useful in the context of reducing the available methods and operator overloading, when subclassing a type. 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. This can be done with (and I hope triple-backquotes markdown works here) : ```py def NotImplementedMethod(self, other): return NotImplemented class ...: ... __le__ = __lt__ = __ge__ = __gt__ = __mul__ = __rmul__ = __add__ = __radd__ = NotImplementedMethod ``` This is very handy, and I think it should be added to the builtins. A simple way to implement this could be to have `NotImplemented(a, b)` return NotImplemented for any value of a and b, so that NotImplemented can be used instead of NotImplementedMethod in the above example. A caveat for that version, and the reason I would not recommand it, is that it makes NotImplemented a callable when it doesn't need to be, and probably shouldn't be. The other way around, which I'm recommanding, is to add the NotImplementedMethod, working as-is but implement it in C, so that it has no inner dict, is lightweight and less prone to programmer mismanagement (making it a builtin_function_or_method instead of a function).

On Tue, 26 Apr 2022 at 09:00, <aanonyme.personne@hotmail.fr> wrote:
This is useful in the context of reducing the available methods and operator overloading, when subclassing a type. 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.
Normally, when you subclass a namedtuple, it's because you want a tuple. Do you perhaps want a dataclass instead? ChrisA

On Mon, Apr 25, 2022 at 03:38:21PM -0000, aanonyme.personne@hotmail.fr wrote:
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

On Tue, 26 Apr 2022 at 09:00, <aanonyme.personne@hotmail.fr> wrote:
This is useful in the context of reducing the available methods and operator overloading, when subclassing a type. 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.
Normally, when you subclass a namedtuple, it's because you want a tuple. Do you perhaps want a dataclass instead? ChrisA

On Mon, Apr 25, 2022 at 03:38:21PM -0000, aanonyme.personne@hotmail.fr wrote:
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
participants (3)
-
aanonyme.personne@hotmail.fr
-
Chris Angelico
-
Steven D'Aprano