<str> = chr(<int>) # Converts int to Unicode character.
<int> = ord(<str>) # Converts Unicode character to int.
<str> = chr(<int>) # Converts passed integer to Unicode character.
<int> = ord(<str>) # Converts passed Unicode character to integer.
```
* **Use `'unicodedata.normalize("NFC", <str>)'` on strings like `'Motörhead'` before comparing them to other strings, because `'ö'` can be stored as one or two characters.**
* **`'NFC'` converts such characters to a single character, while `'NFD'` converts them to two.**
@ -972,9 +972,8 @@ class MyClass:
>>> obj.a, str(obj), repr(obj)
(1, '1', 'MyClass(1)')
```
* **Return value of str() should be readable and of repr() unambiguous.**
* **If only repr() is defined, it will also be used for str().**
* **Methods decorated with `'@staticmethod'` do not receive 'self' nor 'cls' as their first argument.**
* **Methods whose names start and end with two underscores are called special methods. They are executed when object is passed to a built-in function or used as an operand, for example, `'print(a)'` calls `'a.__str__()'` and `'a + b'` calls `'a.__add__(b)'`.**
* **Return value of str() special method should be readable and of repr() unambiguous. If only repr() is defined, it will also be used for str().**
#### Expressions that call the str() method:
```python
@ -982,7 +981,6 @@ print(<obj>)
f'{<obj>}'
logging.warning(<obj>)
csv.writer(<file>).writerow([<obj>])
raise Exception(<obj>)
```
#### Expressions that call the repr() method:
@ -991,11 +989,10 @@ print/str/repr([<obj>])
print/str/repr({<obj>: <obj>})
f'{<obj>!r}'
Z = make_dataclass('Z', ['a']); print/str/repr(Z(<obj>))
>>> <obj>
```
### Subclass
* **Inheritance is a mechanism that enables a class to extend another class (subclass to extend its parent), and by doing so inherit all its methods and attributes.**
* **Inheritance is a mechanism that enables a class to extend some other class (that is, subclass to extend its parent), and by doing so inherit all its methods and attributes.**
* **Subclass can then add its own methods and attributes or override inherited ones by reusing their names.**
```python
@ -1694,7 +1691,7 @@ from pathlib import Path
```
```python
<str> = str(<Path>) # Returns path as str. Also <Path>.as_uri().
<str> = str(<Path>) # Returns path as string. Also <Path>.as_uri().
<file> = open(<Path>) # Also <Path>.read/write_text/bytes(<args>).
```
@ -2765,7 +2762,7 @@ from PIL import Image
<Image> = Image.new('<mode>', (width, height)) # Creates new image. Also `color=<int/tuple>`.
<Image> = Image.open(<path>) # Identifies format based on file's contents.
<Image> = <Image>.convert('<mode>') # Converts image to the new mode (see Modes).
<Image>.save(<path>) # Selects format based on extension (PNG/JPG…).
<Image>.save(<path>) # Accepts `quality=<int>` if extension is jpg.
<Image>.show() # Displays image in default preview app.
```
@ -2788,8 +2785,8 @@ from PIL import Image
```
### Modes
* **`'L'` - Lightness (greyscale image). Each pixel is an int between 0 and 255.**
* **`'RGB'` - Red, green, blue (true color image). Each pixel is a tuple of three ints.**
* **`'L'` - Lightness (greyscale image). Each pixel is an integer between 0 and 255.**
* **`'RGB'` - Red, green, blue (true color image). Each pixel is a tuple of three integers.**
* **`'RGBA'` - RGB with alpha. Low alpha (i.e. forth int) makes pixel more transparent.**
* **`'HSV'` - Hue, saturation, value. Three ints representing color in HSV color space.**
<int> = ord(<str>) <spanclass="hljs-comment"># Converts passed Unicode character to integer.</span>
</code></pre>
<ul>
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'unicodedata.normalize("NFC", <str>)'</span></code> on strings like <codeclass="python hljs"><spanclass="hljs-string">'Motörhead'</span></code> before comparing them to other strings, because <codeclass="python hljs"><spanclass="hljs-string">'ö'</span></code> can be stored as one or two characters.</strong></li>
@ -830,26 +830,23 @@ player = Player(point, direction) <span class="hljs-comment">#
<li><strong>Return value of str() should be readable and of repr() unambiguous.</strong></li>
<li><strong>If only repr() is defined, it will also be used for str().</strong></li>
<li><strong>Methods decorated with <codeclass="python hljs"><spanclass="hljs-string">'@staticmethod'</span></code> do not receive 'self' nor 'cls' as their first argument.</strong></li>
<li><strong>Methods whose names start and end with two underscores are called special methods. They are executed when object is passed to a built-in function or used as an operand, for example, <codeclass="python hljs"><spanclass="hljs-string">'print(a)'</span></code> calls <codeclass="python hljs"><spanclass="hljs-string">'a.__str__()'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'a + b'</span></code> calls <codeclass="python hljs"><spanclass="hljs-string">'a.__add__(b)'</span></code>.</strong></li>
<li><strong>Return value of str() special method should be readable and of repr() unambiguous. If only repr() is defined, it will also be used for str().</strong></li>
</ul>
<div><h4id="expressionsthatcallthestrmethod">Expressions that call the str() method:</h4><pre><codeclass="python language-python hljs">print(<obj>)
<div><h4id="expressionsthatcallthereprmethod">Expressions that call the repr() method:</h4><pre><codeclass="python language-python hljs">print/str/repr([<obj>])
<li><strong>Inheritance is a mechanism that enables a class to extend another class (subclass to extend its parent), and by doing so inherit all its methods and attributes.</strong></li>
<li><strong>Inheritance is a mechanism that enables a class to extend some other class (that is, subclass to extend its parent), and by doing so inherit all its methods and attributes.</strong></li>
<li><strong>Subclass can then add its own methods and attributes or override inherited ones by reusing their names.</strong></li>
<iter> = <Path>.glob(<spanclass="hljs-string">'<pattern>'</span>) <spanclass="hljs-comment"># Returns Paths matching the wildcard pattern.</span>
</code></pre>
<pre><codeclass="python language-python hljs"><str> = str(<Path>) <spanclass="hljs-comment"># Returns path as str. Also <Path>.as_uri().</span>
<pre><codeclass="python language-python hljs"><str> = str(<Path>) <spanclass="hljs-comment"># Returns path as string. Also <Path>.as_uri().</span>
<file> = open(<Path>) <spanclass="hljs-comment"># Also <Path>.read/write_text/bytes(<args>).</span>
@ -2272,7 +2269,7 @@ right = np.array([[<span class="hljs-number">0.1</span>, <span class="hljs-numb
<pre><codeclass="python language-python hljs"><Image> = Image.new(<spanclass="hljs-string">'<mode>'</span>, (width, height)) <spanclass="hljs-comment"># Creates new image. Also `color=<int/tuple>`.</span>
<Image> = Image.open(<path>) <spanclass="hljs-comment"># Identifies format based on file's contents.</span>
<Image> = <Image>.convert(<spanclass="hljs-string">'<mode>'</span>) <spanclass="hljs-comment"># Converts image to the new mode (see Modes).</span>
<Image>.save(<path>) <spanclass="hljs-comment"># Selects format based on extension (PNG/JPG…).</span>
<Image>.save(<path>) <spanclass="hljs-comment"># Accepts `quality=<int>` if extension is jpg.</span>
<Image>.show() <spanclass="hljs-comment"># Displays image in default preview app.</span>
@ -2288,8 +2285,8 @@ right = np.array([[<span class="hljs-number">0.1</span>, <span class="hljs-numb
<Image> = Image.fromarray(np.uint8(<array>)) <spanclass="hljs-comment"># Use <array>.clip(0, 255) to clip the values.</span>
</code></pre>
<div><h3id="modes-1">Modes</h3><ul>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'L'</span></code> - Lightness (greyscale image). Each pixel is an int between 0 and 255.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'RGB'</span></code> - Red, green, blue (true color image). Each pixel is a tuple of three ints.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'L'</span></code> - Lightness (greyscale image). Each pixel is an integer between 0 and 255.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'RGB'</span></code> - Red, green, blue (true color image). Each pixel is a tuple of three integers.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'RGBA'</span></code> - RGB with alpha. Low alpha (i.e. forth int) makes pixel more transparent.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'HSV'</span></code> - Hue, saturation, value. Three ints representing color in HSV color space.</strong></li>
</ul><div><h3id="examples">Examples</h3><div><h4id="createsapngimageofarainbowgradient">Creates a PNG image of a rainbow gradient:</h4><pre><codeclass="python language-python hljs">WIDTH, HEIGHT = <spanclass="hljs-number">100</span>, <spanclass="hljs-number">100</span>
<bool> = <Rect>.colliderect(<Rect>) <spanclass="hljs-comment"># Checks whether the two rectangles overlap.</span>
<int> = <Rect>.collidelist(<list_of_Rect>) <spanclass="hljs-comment"># Returns index of first colliding Rect or -1.</span>
<list> = <Rect>.collidelistall(<list_of_Rect>) <spanclass="hljs-comment"># Returns indices of all colliding rectangles.</span>
</code></pre>
<div><h3id="surface">Surface</h3><p><strong>Object for representing images.</strong></p><pre><codeclass="python language-python hljs"><Surf> = pg.display.set_mode((width, height)) <spanclass="hljs-comment"># Opens new window and returns its surface.</span>
<div><h3id="surface">Surface</h3><p><strong>Object for representing images.</strong></p><pre><codeclass="python language-python hljs"><Surf> = pg.display.set_mode((width, height)) <spanclass="hljs-comment"># Opens a new window and returns its surface.</span>
<Surf> = pg.Surface((width, height)) <spanclass="hljs-comment"># New RGB surface. RGBA if `flags=pg.SRCALPHA`.</span>
<Surf> = pg.image.load(<path/file>) <spanclass="hljs-comment"># Loads the image. Format depends on source.</span>
<Surf> = pg.surfarray.make_surface(<np_array>) <spanclass="hljs-comment"># Also `<np_arr> = surfarray.pixels3d(<Surf>)`.</span>
@ -2487,13 +2484,13 @@ pg.quit()
<Surf>.set_at((x, y), color) <spanclass="hljs-comment"># Updates pixel. Also <Surf>.get_at((x, y)).</span>
<Surf>.blit(<Surf>, (x, y)) <spanclass="hljs-comment"># Draws passed surface at specified location.</span>
<Surf> = rotate(<Surf>, angle) <spanclass="hljs-comment"># Rotates the surface for counterclock degrees.</span>
<Surf> = flip(<Surf>, flip_x=<spanclass="hljs-keyword">False</span>) <spanclass="hljs-comment"># Mirrors the surface. Also `flip_y=False`.</span>