Debian Codegraph @

docs/internals.md

raw
1 # Internals
2 ## How the magic of `DirtyEquals.__eq__` works?
3 When you call `x == y`, Python first calls `x.__eq__(y)`. This would not help us
4 much, because we would have to keep an eye on order of the arguments when
5 comparing to `DirtyEquals` objects. But that's where were another feature of
6 Python comes in.
7
8 When `x.__eq__(y)` returns the `NotImplemented` object, then Python will try to
9 call `y.__eq__(x)`. Objects in the standard library return that value when they
10 don't know how to compare themselves to objects of `type(y)` (Without checking
11 the C source I can't be certain if this assumption holds for all classes, but it
12 works for all the basic ones).
13 In [`pathlib.PurePath`](https://github.com/python/cpython/blob/aebbd7579a421208f48dd6884b67dbd3278b71ad/Lib/pathlib.py#L751)
14 you can see an example how that is implemented in Python.
15
16 > By default, object implements `__eq__()` by using `is`,
17 > returning `NotImplemented` in the case of a false comparison:
18 > `True if x is y else NotImplemented`.
19
20 See the Python documentation for more information ([`object.__eq__`](https://docs.python.org/3/reference/datamodel.html#object.__eq__)).