<dict> = vars(<object>) # Returns dict of object's fields. Also <obj>.__dict__.
<list> = dir(<object>) # Names of object's attributes (incl. methods).
<dict> = vars(<object>) # Dict of object's fields. Also <obj>.__dict__.
```
```python
@ -2198,7 +2198,7 @@ class MyMetaClass(type):
* **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
**Right before a class is created it checks if it has a '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 his parents has it defined and eventually comes to type().**
```python
class MyClass(metaclass=MyMetaClass):
@ -2223,8 +2223,8 @@ type(MyMetaClass) == type # MyMetaClass is an instance of type.
| MyClass --> MyMetaClass |
| | v |
| object -----> type <+ |
| | ^ +---+ |
| str ---------+ |
| | ^ +--+ |
| str ----------+ |
+-------------+-------------+
```
@ -2388,7 +2388,7 @@ def get_border(screen):
from collections import namedtuple
P = namedtuple('P', 'x y')
height, width = screen.getmaxyx()
return P(width-1, height-1)
return P(width-1, height-1)
if __name__ == '__main__':
main()
@ -2775,7 +2775,7 @@ for velocity in range(15):
y = sum(range(velocity+1))
frame = Image.new('L', (WIDTH, WIDTH))
draw = ImageDraw.Draw(frame)
draw.ellipse((WIDTH/2-R, y, WIDTH/2+R, y+2*R), fill='white')
draw.ellipse((WIDTH/2-R, y, WIDTH/2+R, y+R*2), fill='white')
<div><h2id="exit"><ahref="#exit"name="exit">#</a>Exit</h2><p><strong>Exits the interpreter by raising SystemExit exception.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> sys
sys.exit() <spanclass="hljs-comment"># Exits with exit code 0 (success).</span>
sys.exit(<el>) <spanclass="hljs-comment"># Prints object to stderr and exits with 1.</span>
sys.exit(<int>) <spanclass="hljs-comment"># Exits with the passed exit code.</span>
sys.exit(<int>) <spanclass="hljs-comment"># Exits with passed exit code.</span>
<li><strong>Levels deeper than 'depth' get replaced by '…'.</strong></li>
<li><strong>Levels deeper than 'depth' get replaced with '…'.</strong></li>
</ul>
<div><h2id="input"><ahref="#input"name="input">#</a>Input</h2><p><strong>Reads a line from user input or pipe if present.</strong></p><pre><codeclass="python language-python hljs"><str> = input(prompt=<spanclass="hljs-keyword">None</span>)
<div><h2id="input"><ahref="#input"name="input">#</a>Input</h2><p><strong>Reads a line from the user input or pipe if present.</strong></p><pre><codeclass="python language-python hljs"><str> = input(prompt=<spanclass="hljs-keyword">None</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"># Returns names of local variables (incl. functions).</span>
<dict> = vars() <spanclass="hljs-comment"># Returns dict of local variables. Also locals().</span>
<dict> = globals() <spanclass="hljs-comment"># Returns dict of global variables.</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>
<dict> = vars() <spanclass="hljs-comment"># Dict of local variables. Also locals().</span>
<dict> = globals() <spanclass="hljs-comment"># Dict of global variables.</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>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>
<div><h3id="metaclassattribute">Metaclass Attribute</h3><p><strong>Right before a class is created it checks if it has a '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 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>
b = <spanclass="hljs-number">12345</span>
</code></pre></div>
@ -1934,8 +1934,8 @@ type(MyMetaClass) == type <span class="hljs-comment"># MyMetaClass is
| MyClass --> MyMetaClass |
| | v |
| object -----> type <+ |
| | ^ +---+ |
| str ---------+ |
| | ^ +--+ |
| str ----------+ |
+-------------+-------------+
</code></pre>
<div><h3id="inheritancediagram">Inheritance Diagram</h3><pre><codeclass="python language-python hljs">MyClass.__base__ == object <spanclass="hljs-comment"># MyClass is a subclass of object.</span>
draw.ellipse((WIDTH/<spanclass="hljs-number">2</span>-R, y, WIDTH/<spanclass="hljs-number">2</span>+R, y+<spanclass="hljs-number">2</span>*R), fill=<spanclass="hljs-string">'white'</span>)
draw.ellipse((WIDTH/<spanclass="hljs-number">2</span>-R, y, WIDTH/<spanclass="hljs-number">2</span>+R, y+R*<spanclass="hljs-number">2</span>), fill=<spanclass="hljs-string">'white'</span>)
x = math.copysign(abs(x) - <spanclass="hljs-number">1</span>, x) <spanclass="hljs-keyword">if</span> x <spanclass="hljs-keyword">else</span><spanclass="hljs-number">0</span>
x -= x / abs(x) <spanclass="hljs-keyword">if</span> x <spanclass="hljs-keyword">else</span><spanclass="hljs-number">0</span>