[issue22978] Logical Negation of NotImplemented
New submission from Maytag Metalark: Performing a logical negation (`not`) on `NotImplemented` should return `NotImplemented`. Currently, it returns `False`. A common pattern for implementing __eq__ and __ne__ is to implement the comparison in __eq__, and simply delegate to it in __ne__ with a negation. However, if two values are incomparable, then __eq__ and __ne__ should both return NotImplemented. If you try to negate NotImplemented in __ne__, you will end up with a value of False, instead of NotImplemented, so you have to specifically test for this case. For instance, here is how one would write the code now: def __ne__(self, other): eq = self.__eq__(other) if eq is NotImplemented: return NotImplemented return not eq Where as the following would be simpler, and could be used if this change was made: def __ne__(self, other): return not self.__eq__(other) This is not simply sugar to reduce typing, it is safer because some coders may forget about NotImplemented and implement __ne__ as shown in the second example anyway, which is not actually correct with the current behavior. -- messages: 231996 nosy: Brian.Mearns priority: normal severity: normal status: open title: Logical Negation of NotImplemented type: enhancement versions: Python 2.7 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22978 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22979] Use of None in min and max
New submission from Maytag Metalark: `None` should never be the result of the built-in `min` and `max` functions. When `None` is supplied as one of the values to check, it should never be chosen as the result. This would make it much easier to find a minimum and/or maximum while iterating over values. For instance, the following is a common pattern: mn = None mx = None for x in iterable: if mn is None or x mn: mn = x if mx is None or x mx: mx = x Note that although the `min` and `max` functions could be applied directly to `iterable` in the above case, the above pattern is more efficient (only once through the loop) and covers the common case where additional operations are performed on each value of the iterable. If the suggested enhancement was made, the above code could be written more simply as: mn = None mx = None for x in iterable: mn = min(mn, x) mx = max(mx, x) At present, this will actually work for `max`, as None evaluates as less than every number, but it will not work for `min` (for the same reason). The suggested change would mean that None is simultaneously greater than and less than every other value, but that only matters if we assume a total ordering of all the values including None, which doesn't seem like it would be important. -- messages: 231998 nosy: Brian.Mearns priority: normal severity: normal status: open title: Use of None in min and max type: enhancement versions: Python 2.7 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22979 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com