Created
January 24, 2022 13:18
-
-
Save atombrella/c16693f564b4bbf375f70e5d56fe30ad to your computer and use it in GitHub Desktop.
Python __eq__ and __ne__ experiments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Any | |
class Something: | |
def __init__(self, *, a: int = 4) -> None: | |
self.a = a | |
def __eq__(self, other: Any) -> bool: | |
if isinstance(other, self.__class__): | |
return self.a == other.a | |
return NotImplemented | |
class OtherClass: | |
def __init__(self, b : bool = False) -> None: | |
self.b = b | |
def __eq__(self, other: Any) -> bool: | |
if isinstance(other, self.__class__): | |
return self.b == other.b | |
return NotImplemented | |
if __name__ == '__main__': | |
something = Something(a=5) | |
other = OtherClass(b=True) | |
print(other == something) # False | |
print(something == something) # True | |
print(something != other) # True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment