* **Only required methods are iter() and len(). Len() should return the number of items.**
* **Only required methods are iter() and len(). Len() should return the number of items.**
* **This cheatsheet actually means `'<iterable>'` when it uses `'<collection>'`.**
* **This cheatsheet actually means `'<iterable>'` when it uses `'<collection>'`.**
* **I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only drawback of this decision is that a reader could think a certain function doesn't accept iterators when it does, since iterators are the only built-in objects that are iterable but are not collections.**
* **I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only drawback of this decision is that the reader could think a certain function doesn't accept iterators when it does, since iterators are the only built-in objects that are iterable but are not collections.**
```python
```python
class MyCollection:
class MyCollection:
def __init__(self, a):
def __init__(self, a):
@ -1606,7 +1606,7 @@ Open
<file>.writelines(<collection>) # Writes a coll. of strings or bytes objects.
<file>.writelines(<collection>) # Writes a coll. of strings or bytes objects.
<file>.flush() # Flushes write buffer. Runs every 4096/8192 B.
<file>.flush() # Flushes write buffer. Runs every 4096/8192 B.
```
```
* **Methods do not add or strip trailing newlines, even writelines().**
* **Methods do not add or strip trailing newlines, not even writelines().**
### Read Text from File
### Read Text from File
```python
```python
@ -2045,7 +2045,7 @@ from array import array
Memory View
Memory View
-----------
-----------
* **A sequence object that points to the memory of another object.**
* **A sequence object that points to the memory of another bytes-like object.**
* **Each element can reference a single or multiple consecutive bytes, depending on format.**
* **Each element can reference a single or multiple consecutive bytes, depending on format.**
* **Order and number of elements can be changed with slicing.**
* **Order and number of elements can be changed with slicing.**
* **Casting only works between char and other types and uses system's sizes.**
* **Casting only works between char and other types and uses system's sizes.**
* **Binary operators require objects to have and(), or(), xor() and invert() special methods, unlike logical operators that work on all types of objects.**
* **Bitwise operators require objects to have and(), or(), xor() and invert() special methods, unlike logical operators that work on all types of objects.**
* **The only difference between the examples above is that my\_meta\_class() returns a class of type type, while MyMetaClass() returns a class of type MyMetaClass.**
* **The only difference between the examples above is that my\_meta\_class() returns a class of type type, while MyMetaClass() returns a class of type MyMetaClass.**
### Metaclass Attribute
### Metaclass Attribute
**Right before a class is created it checks if it has the 'metaclass' attribute defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().**
**Right before a class is created it checks if it has the 'metaclass' attribute defined. If not, it recursively checks if any of its parents has it defined and eventually comes to type().**
* **Starts the app at `'http://localhost:5000'`. Use `'host="0.0.0.0"'` to run externally.**
* **Starts the app at `'http://localhost:5000'`. Use `'host="0.0.0.0"'` to run externally.**
* **Install a WSGI server like [Waitress](https://flask.palletsprojects.com/en/latest/deploying/waitress/) and a HTTP server such as [Nginx](https://flask.palletsprojects.com/en/latest/deploying/nginx/) for better security.**
* **Install a WSGI server like [Waitress](https://flask.palletsprojects.com/en/latest/deploying/waitress/) and a HTTP server such as [Nginx](https://flask.palletsprojects.com/en/latest/deploying/nginx/) for better security.**
<li><strong>Only required methods are iter() and len(). Len() should return the number of items.</strong></li>
<li><strong>Only required methods are iter() and len(). Len() should return the number of items.</strong></li>
<li><strong>This cheatsheet actually means <codeclass="python hljs"><spanclass="hljs-string">'<iterable>'</span></code> when it uses <codeclass="python hljs"><spanclass="hljs-string">'<collection>'</span></code>.</strong></li>
<li><strong>This cheatsheet actually means <codeclass="python hljs"><spanclass="hljs-string">'<iterable>'</span></code> when it uses <codeclass="python hljs"><spanclass="hljs-string">'<collection>'</span></code>.</strong></li>
<li><strong>I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only drawback of this decision is that a reader could think a certain function doesn't accept iterators when it does, since iterators are the only built-in objects that are iterable but are not collections.</strong></li>
<li><strong>I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only drawback of this decision is that the reader could think a certain function doesn't accept iterators when it does, since iterators are the only built-in objects that are iterable but are not collections.</strong></li>
<file>.flush() <spanclass="hljs-comment"># Flushes write buffer. Runs every 4096/8192 B.</span>
<file>.flush() <spanclass="hljs-comment"># Flushes write buffer. Runs every 4096/8192 B.</span>
</code></pre>
</code></pre>
<ul>
<ul>
<li><strong>Methods do not add or strip trailing newlines, even writelines().</strong></li>
<li><strong>Methods do not add or strip trailing newlines, not even writelines().</strong></li>
</ul>
</ul>
<div><h3id="readtextfromfile">Read Text from File</h3><pre><codeclass="python language-python hljs"><spanclass="hljs-function"><spanclass="hljs-keyword">def</span><spanclass="hljs-title">read_file</span><spanclass="hljs-params">(filename)</span>:</span>
<div><h3id="readtextfromfile">Read Text from File</h3><pre><codeclass="python language-python hljs"><spanclass="hljs-function"><spanclass="hljs-keyword">def</span><spanclass="hljs-title">read_file</span><spanclass="hljs-params">(filename)</span>:</span>
<li><strong>Binary operators require objects to have and(), or(), xor() and invert() special methods, unlike logical operators that work on all types of objects.</strong></li>
<li><strong>Bitwise operators require objects to have and(), or(), xor() and invert() special methods, unlike logical operators that work on all types of objects.</strong></li>
<div><h2id="introspection"><ahref="#introspection"name="introspection">#</a>Introspection</h2><p><strong>Inspecting code at runtime.</strong></p><div><h3id="variables">Variables</h3><pre><codeclass="python language-python hljs"><list> = dir() <spanclass="hljs-comment"># Names of local variables (incl. functions).</span>
<div><h2id="introspection"><ahref="#introspection"name="introspection">#</a>Introspection</h2><p><strong>Inspecting code at runtime.</strong></p><div><h3id="variables">Variables</h3><pre><codeclass="python language-python hljs"><list> = dir() <spanclass="hljs-comment"># Names of local variables (incl. functions).</span>
<li><strong>Like in our case, new() can also be called directly, usually from a new() method of a child class (</strong><codeclass="python hljs"><spanclass="hljs-function"><spanclass="hljs-keyword">def</span><spanclass="hljs-title">__new__</span><spanclass="hljs-params">(cls)</span>:</span><spanclass="hljs-keyword">return</span> super().__new__(cls)</code><strong>).</strong></li>
<li><strong>Like in our case, new() can also be called directly, usually from a new() method of a child class (</strong><codeclass="python hljs"><spanclass="hljs-function"><spanclass="hljs-keyword">def</span><spanclass="hljs-title">__new__</span><spanclass="hljs-params">(cls)</span>:</span><spanclass="hljs-keyword">return</span> super().__new__(cls)</code><strong>).</strong></li>
<li><strong>The only difference between the examples above is that my_meta_class() returns a class of type type, while MyMetaClass() returns a class of type MyMetaClass.</strong></li>
<li><strong>The only difference between the examples above is that my_meta_class() returns a class of type type, while MyMetaClass() returns a class of type MyMetaClass.</strong></li>
</ul>
</ul>
<div><h3id="metaclassattribute">Metaclass Attribute</h3><p><strong>Right before a class is created it checks if it has the 'metaclass' attribute defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-class"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">MyClass</span><spanclass="hljs-params">(metaclass=MyMetaClass)</span>:</span>
<div><h3id="metaclassattribute">Metaclass Attribute</h3><p><strong>Right before a class is created it checks if it has the 'metaclass' attribute defined. If not, it recursively checks if any of its parents has it defined and eventually comes to type().</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-class"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">MyClass</span><spanclass="hljs-params">(metaclass=MyMetaClass)</span>:</span>
<li><strong>Starts the app at <codeclass="python hljs"><spanclass="hljs-string">'http://localhost:5000'</span></code>. Use <codeclass="python hljs"><spanclass="hljs-string">'host="0.0.0.0"'</span></code> to run externally.</strong></li>
<li><strong>Starts the app at <codeclass="python hljs"><spanclass="hljs-string">'http://localhost:5000'</span></code>. Use <codeclass="python hljs"><spanclass="hljs-string">'host="0.0.0.0"'</span></code> to run externally.</strong></li>