0% found this document useful (0 votes)
2 views2 pages

Reference Python

The document outlines the use of magic methods in Python, detailing how various operators and expressions are associated with specific methods for objects of a common class. It includes a comprehensive table of these methods for both unary and binary operations, as well as function wrapping concepts. Additionally, it briefly describes operations for lists and dictionaries, including addition, multiplication, and membership checks.

Uploaded by

yiyayi7296
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Reference Python

The document outlines the use of magic methods in Python, detailing how various operators and expressions are associated with specific methods for objects of a common class. It includes a comprehensive table of these methods for both unary and binary operations, as well as function wrapping concepts. Additionally, it briefly describes operations for lists and dictionaries, including addition, multiplication, and membership checks.

Uploaded by

yiyayi7296
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

```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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy