Skip to content

Commit 4a016e5

Browse files
docs(dundersr): Allign docs and clean redaction.
1 parent 4902dac commit 4a016e5

1 file changed

Lines changed: 36 additions & 36 deletions

File tree

docs/pages/language/dunders.md

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ Dunders (`__add__`, `__eq__`, `__getitem__`, ...) plug a class into language pro
77

88
```python
99
class V:
10-
def __init__(self, n):
11-
self.n = n
12-
def __add__(self, o):
13-
return V(self.n + o.n)
14-
def __eq__(self, o):
15-
return self.n == o.n
10+
def __init__(self, n):
11+
self.n = n
12+
def __add__(self, o):
13+
return V(self.n + o.n)
14+
def __eq__(self, o):
15+
return self.n == o.n
1616

1717
print((V(3) + V(4)).n)
1818
print(V(3) == V(3))
@@ -23,7 +23,7 @@ print(V(3) == V(3))
2323
True
2424
```
2525

26-
Dunders are looked up on the class chain (instance dict skipped). Subclasses inherit and may override, operator overloading composes with [single-level inheritance](/language/classes#inheritance-and-super). Monomorphic sites (same class for both operands) promote through the IC after 4 hits and bypass lookup entirely.
26+
Dunders are looked up on the class chain (instance dict skipped). Subclasses inherit and may override; operator overloading composes with [single-level inheritance](/language/classes#inheritance-and-super). Monomorphic sites (same class for both operands) promote through the IC after 4 hits and bypass lookup entirely.
2727

2828
## Arithmetic
2929

@@ -36,19 +36,19 @@ Dunders are looked up on the class chain (instance dict skipped). Subclasses inh
3636
| `a // b` | `__floordiv__` | `__rfloordiv__` |
3737
| `a % b` | `__mod__` | `__rmod__` |
3838
| `a ** b` | `__pow__` | `__rpow__` |
39-
| `-a` | `__neg__` |, |
39+
| `-a` | `__neg__` | |
4040

4141
Returning `NotImplemented` from the forward op tells the VM to try the reflected op on the other operand. Both `NotImplemented` (or neither defined) -> `TypeError`.
4242

43-
Subclass-first: when `type(b)` is a strict subclass of `type(a)`, `b.__radd__` runs before `a.__add__`, lets a subclass override an inherited reflected op without touching the base.
43+
Subclass-first: when `type(b)` is a strict subclass of `type(a)`, `b.__radd__` runs before `a.__add__` — letting a subclass override an inherited reflected op without touching the base.
4444

4545
```python
4646
class Money:
47-
def __init__(self, n): self.n = n
48-
def __add__(self, o):
49-
return Money(self.n + (o.n if isinstance(o, Money) else o))
50-
def __radd__(self, o):
51-
return Money(o + self.n)
47+
def __init__(self, n): self.n = n
48+
def __add__(self, o):
49+
return Money(self.n + (o.n if isinstance(o, Money) else o))
50+
def __radd__(self, o):
51+
return Money(o + self.n)
5252

5353
print((Money(10) + Money(5)).n)
5454
print((3 + Money(7)).n)
@@ -70,7 +70,7 @@ print((3 + Money(7)).n)
7070
| `a > b` | `__gt__` | `__lt__` |
7171
| `a >= b` | `__ge__` | `__le__` |
7272

73-
`!=` falls back to `not __eq__` when `__ne__` is absent. Results coerce to `bool`, `__lt__` returning `'A.lt'` yields `True`, not the string.
73+
`!=` falls back to `not __eq__` when `__ne__` is absent. Results coerce to `bool` `__lt__` returning `'A.lt'` yields `True`, not the string.
7474

7575
## Truth and length
7676

@@ -84,13 +84,13 @@ print((3 + Money(7)).n)
8484

8585
```python
8686
class Empty:
87-
def __bool__(self):
88-
return False
87+
def __bool__(self):
88+
return False
8989

9090
class Container:
91-
def __init__(self, n): self.n = n
92-
def __len__(self):
93-
return self.n
91+
def __init__(self, n): self.n = n
92+
def __len__(self):
93+
return self.n
9494

9595
print(bool(Empty()))
9696
print(bool(Container(0)), bool(Container(3)))
@@ -125,16 +125,16 @@ Absent `__contains__`: `v in obj` falls back to iterating `obj` with `__eq__`.
125125

126126
```python
127127
class Up:
128-
def __init__(self, stop):
129-
self.i = 0
130-
self.stop = stop
131-
def __iter__(self):
132-
return self
133-
def __next__(self):
134-
if self.i >= self.stop:
135-
raise StopIteration
136-
self.i += 1
137-
return self.i
128+
def __init__(self, stop):
129+
self.i = 0
130+
self.stop = stop
131+
def __iter__(self):
132+
return self
133+
def __next__(self):
134+
if self.i >= self.stop:
135+
raise StopIteration
136+
self.i += 1
137+
return self.i
138138

139139
print(list(Up(3)))
140140
```
@@ -151,8 +151,8 @@ print(list(Up(3)))
151151

152152
```python
153153
class Double:
154-
def __call__(self, x):
155-
return x * 2
154+
def __call__(self, x):
155+
return x * 2
156156

157157
d = Double()
158158
print(d(7))
@@ -168,7 +168,7 @@ True
168168

169169
`hash(x)` calls `__hash__`; must return `int` (masked to `INT_MAX`).
170170

171-
Eq/hash invariant: a class defining `__eq__` without `__hash__` is unhashable, `hash(x)` and `{x: 1}` raise `TypeError`. Prevents inconsistent dict keys.
171+
Eq/hash invariant: a class defining `__eq__` without `__hash__` is unhashable `hash(x)` and `{x: 1}` raise `TypeError`. Prevents inconsistent dict keys.
172172

173173
```python
174174
class K:
@@ -198,7 +198,7 @@ Built-in dict/set still compare instance keys by identity (`Val` bits); user `__
198198
| `str(x)`, `print(x)`| `__str__` | `__repr__`, then default |
199199
| `f"{x}"` (no spec) | `__str__` | same as `str(x)` |
200200
| `f"{x:spec}"` | `__format__` | built-in format spec engine |
201-
| `f"{x!r}"` | `__repr__` |, |
201+
| `f"{x!r}"` | `__repr__` | |
202202

203203
`__format__(spec)` receives the spec string and must return `str`.
204204

@@ -225,7 +225,7 @@ Existing attributes bypass `__getattr__`; only misses trigger it.
225225

226226
## Context managers
227227

228-
`with cm() as x:` invokes `__enter__`; its return binds to `as`. On exit, `__exit__(exc_type, exc_value, traceback)` runs, `(None, None, None)` for normal exit, live exception info on raise. Truthy return suppresses; falsy propagates.
228+
`with cm() as x:` invokes `__enter__`; its return binds to `as`. On exit, `__exit__(exc_type, exc_value, traceback)` runs `(None, None, None)` for normal exit, live exception info on raise. Truthy return suppresses; falsy propagates.
229229

230230
```python
231231
class Suppress:
@@ -243,7 +243,7 @@ print("after")
243243
after
244244
```
245245

246-
Multiple managers (`with a(), b() as x:`) nest LIFO, `b` enters last, exits first. Each has its own implicit handler, so inner suppression still lets outer managers run their normal `__exit__(None, None, None)`.
246+
Multiple managers (`with a(), b() as x:`) nest LIFO `b` enters last, exits first. Each has its own implicit handler, so inner suppression still lets outer managers run their normal `__exit__(None, None, None)`.
247247

248248
If `__exit__` itself raises, the new exception replaces the original.
249249

0 commit comments

Comments
 (0)