Skip to content

Commit d4e5a77

Browse files
authored
Document how the dirty __eq__ is called (#41)
* Document how the dirt __eq__ is called * Move and reformat Internals docs * fix typos * include internals.md in mkdocs.yml
1 parent 2d3c0e5 commit d4e5a77

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,4 @@ pip install dirty-equals
9797
```
9898

9999
**dirty-equals** requires **Python 3.7+**.
100+

docs/internals.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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__)).

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ nav:
4444
- types/boolean.md
4545
- types/other.md
4646
- types/custom.md
47+
- Internals: internals.md
4748

4849
markdown_extensions:
4950
- toc:

0 commit comments

Comments
 (0)