```python
M = 3
print(M)
```
Provisionally use $\langle a, b, c, \ldots\rangle$ to denote meta-lists. $[a, b, c,
\ldots]$ denotes an ordinary list.
$-$ denotes main metavariable
### Table of Magic Methods
Note: `x` and `y` are presumed objects of a common class, and method calls like
`x.__add__(self, y)` will be written as `add(y)`.
Note: infix operators (ex. `x#y`) often have fallbacks in which `y` gets to run
`rhash(x)` if `x` can't run `hash(y)`. These methods (`radd`, `rmul`, etc.) aren't
tabulated here.
| | Expression | Method | Expression | Method
|
| -------------- | ------------ | ----------------- | ---------- | ---------------
|
| 1-Place Prefix | `+x` | `x.pos()` | `-` | `neg`
|
| | `~x` | `invert` | |
|
| 2-Place Infix | `x+y` | `x.add(y)` | `-` | `sub`
|
| | `//` | `floordiv` | `/` | `truediv`
|
| | `*` | `mul` | `**` | `pow`
|
| | `%` | `mod` | `@` | `matmul`
|
| | `<` | `lt` | `>` | `gt`
|
| | `<<` | `lshift` | `>>` | `rshift`
|
| | `<=` | `le` | `>=` | `ge`
|
| | `<<=` | `ilshift` | `>>=` | `irshift`
|
| | `&` | `and` | `&=` | `iand`
|
| | `^` | `xor` | `^=` | `ixor`
|
| | `\|` | `or` | `\|=` | `ior`
|
| | `+=` | `iadd` | `-=` | `isub`
|
| | `*=` | `imul` | `**=` | `ipow`
|
| | `//=` | `ifloordiv` | `/=` | `itruediv`
|
| | `%=` | `imod` | `@=` | `imatmul`
|
| | `==` | `eq` | `!=` | `ne`
|
| 1-Place Call | `L[x]` | `L[x.index()]` | | |
| 2-Place Call | `x.a` | `getattr("a")` | `x(*a)` | `call(*a)`
|
| | `x[a]` | `getitem(a)` | |
|
| 3-Place Assign | `x.a = y` | `setattr("a", y)` | `x[a] = y` | `setitem(a, y)`
|
| Membership | `i in x` | `x.contains(i)` | |
|
| Iteration | `for i in x` | `x.iter()` | |
|
Multi-key lookup like `x[a,b,c]` is parsed as `x.__getitem__((a, b, c))`.
#### Uses for Function Wrapping
If we have a class `Fun` which wraps functions like `Fun(lambda x: x**2)`, then the
class **could** have its instantiations (`F, G, ...` wrapping functions `f,
g, ...`) implement magic methods as such:
- `F(*a)` just calls `f(*a)`
- `F*G` sends `a` to `F(G(a))`, while `F@G` sends `a` to `G(F(a))` like $f\circ g$.
- `~F` applies the map functional, so that `~F([a,b,...])` evaluates to
`[F(a),F(b),...]`.
- `(F/G)(a)` works as `try: F(a); except: G(a)`
- `+` and `-` zip and unzip arguments, so that `-F([a,b,...])` evaluates to
`F(a,b,...)` and `+F(a,b,...)` evaluates to `F([a,b,...])`
- `F**n`, for `n` a positive integer, does `n`-fold application of `F`.
## Lists
With $A = [a_1, \ldots, a_m]$ and $B = [b_1, \ldots, b_n]$,
$A + B$ should be $[a_1, \ldots, a_m, b_1, \ldots, b_n]$,
$A \times B$ should be $[(a_1, b_1), \ldots, (a_m, b_1), \ldots, (a_1, b_n), \
ldots, (a_m, b_n)]$
$A - B$ should be $[x \in A \mid x \notin B]$
$A\ \&\ B$ should be $[x \in A \mid x \in B]$.
## Dictionaries