Skip to content

Instantly share code, notes, and snippets.

@atombrella
Created January 24, 2022 13:18
Show Gist options
  • Save atombrella/c16693f564b4bbf375f70e5d56fe30ad to your computer and use it in GitHub Desktop.
Save atombrella/c16693f564b4bbf375f70e5d56fe30ad to your computer and use it in GitHub Desktop.
Python __eq__ and __ne__ experiments
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