Return isinstance(other, BaseEquatable) and self.x = other.x Demonstration of unexpected behavior when defining _ne_ based on _eq_:įirst the setup: class BaseEquatable(object): That is, both of the above functions should always return the same result. """always should return same as negation_of_equals(inst1, inst2)""" """always should return same as not_equals(inst1, inst2)""" Semantically, you should implement _ne_ in terms of the check for equality because users of your class will expect the following functions to be equivalent for all instances of A.: def negation_of_equals(inst1, inst2): (It does not check for the same method twice.) Using the = operator allows for this logic to take place. If NotImplemented is returned, then it uses the opposite's method. When given a rich comparison operator, if they're not the same type, Python checks if the other is a subtype, and if it has that operator defined, it uses the other's method first (inverse for = and >). Some other fallback, depending on the operator.) Its truth value is (The interpreter will then try the reflected operation, or This value if they do not implement the operation for the operands Numeric methods and rich comparison methods may return This object is accessed through the built-in name There is a single object with this value. != now returns the opposite of =, unless = returns NotImplemented.įor implementing _ne_, we prefer to use the = operator instead of using the _eq_ method directly so that if self._eq_(other) of a subclass returns NotImplemented for the type checked, Python will appropriately check other._eq_(self) From the documentation: The NotImplemented object.This section of the documentation has been updated for Python 3:īy default, _ne_() delegates to _eq_() and inverts the resultĪnd in the "what's new" section, we see this behavior has changed: So that means that if we define _ne_ in terms of the inverse of _eq_, we can get consistent behavior. Accordingly, whenĭefining _eq_(), one should also define _ne_() so that the Truth of x=y does not imply that x!=y is false. There are no implied relationships among the comparison operators. Provides incorrect behavior in the demonstration below. not implementing _ne_ in Python 2 at all.implementing _ne_() operator based on _eq_ and.Return not self = other # NOT `return not self._eq_(other)` In Python 2, Python itself does not automatically implement any operation in terms of another - therefore, you should define the _ne_ in terms of = instead of the _eq_. If you need your code to work in Python 2, follow the recommendation for Python 2 and it will work in Python 3 just fine. Inheriting from a builtin, it's best to override both. _ne_ isn't already defined in a superclass. The _ne_ method follows automatically from _eq_ only if That is, keep in mind Raymond Hettinger's comment: Generally speaking, for Python 3-only code, don't write one unless you need to overshadow the parent implementation, e.g. In Python 3, != is the negation of = by default, so you are not even required to write a _ne_, and the documentation is no longer opinionated on writing one. Python, should I implement _ne_() operator based on _eq_? Short Answer: Don't implement it, but if you must, use =, not _eq_
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |