|
1123 | 1123 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__enter__</span><span class="hljs-params">(self)</span>:</span>
|
1124 | 1124 | self.file = open(self.filename)
|
1125 | 1125 | <span class="hljs-keyword">return</span> self.file
|
1126 |
| - <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__exit__</span><span class="hljs-params">(self, exc_type, exc_value, traceback)</span>:</span> |
| 1126 | + <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__exit__</span><span class="hljs-params">(self, exc_type, exception, traceback)</span>:</span> |
1127 | 1127 | self.file.close()
|
1128 | 1128 | </code></pre></div>
|
1129 | 1129 |
|
|
1256 | 1256 | Cutlery = Enum(<span class="hljs-string">'Cutlery'</span>, {<span class="hljs-string">'fork'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'knife'</span>: <span class="hljs-number">2</span>, <span class="hljs-string">'spoon'</span>: <span class="hljs-number">3</span>})
|
1257 | 1257 | </code></pre></div>
|
1258 | 1258 |
|
1259 |
| -<div><h4 id="functionscannotbevaluessotheymustbewrapped">Functions can not be values, so they must be wrapped:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> partial |
| 1259 | +<div><h4 id="userdefinedfunctionscannotbevaluessotheymustbewrapped">User-defined functions can not be values, so they must be wrapped:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> partial |
1260 | 1260 | LogicOp = Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-string">'AND'</span>: partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">and</span> r),
|
1261 | 1261 | <span class="hljs-string">'OR'</span> : partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">or</span> r)})
|
1262 | 1262 | </code></pre></div>
|
1263 | 1263 |
|
1264 | 1264 | <ul>
|
1265 |
| -<li><strong>Another solution in this particular case, is to use <code class="python hljs"><span class="hljs-string">'and_'</span></code> and <code class="python hljs"><span class="hljs-string">'or_'</span></code> functions from module <a href="#operator">operator</a>.</strong></li> |
| 1265 | +<li><strong>Another solution in this particular case is to use built-in functions <code class="python hljs"><span class="hljs-string">'and_'</span></code> and <code class="python hljs"><span class="hljs-string">'or_'</span></code> from the module <a href="#operator">operator</a>.</strong></li> |
1266 | 1266 | </ul>
|
1267 | 1267 | <div><h2 id="exceptions"><a href="#exceptions" name="exceptions">#</a>Exceptions</h2><div><h3 id="basicexample">Basic Example</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">try</span>:
|
1268 | 1268 | <code>
|
|
1297 | 1297 | <span class="hljs-keyword">raise</span> <exception>(<el> [, ...])
|
1298 | 1298 | </code></pre></div>
|
1299 | 1299 |
|
1300 |
| -<div><h4 id="reraisingcaughtexception">Re-raising caught exception:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> <exception>: |
1301 |
| - <code> |
| 1300 | +<div><h4 id="reraisingcaughtexception">Re-raising caught exception:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> <exception> <span class="hljs-keyword">as</span> <name>: |
| 1301 | + ... |
1302 | 1302 | <span class="hljs-keyword">raise</span>
|
1303 | 1303 | </code></pre></div>
|
1304 | 1304 |
|
1305 |
| -<div><h4 id="usefulbuiltinexceptions">Useful built-in exceptions:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">raise</span> TypeError(<span class="hljs-string">'Argument is of wrong type!'</span>) |
1306 |
| -<span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">'Argument is of right type but inappropriate value!'</span>) |
1307 |
| -<span class="hljs-keyword">raise</span> RuntimeError(<span class="hljs-string">'None of above!'</span>) |
| 1305 | +<div><h3 id="attributes-1">Attributes</h3><pre><code class="python language-python hljs">arguments = <name>.args |
| 1306 | +line_num = <name>.__traceback__.tb_lineno |
| 1307 | +func_name = <name>.__traceback__.tb_frame.f_code.co_name |
| 1308 | +filename = <name>.__traceback__.tb_frame.f_code.co_filename |
| 1309 | +line = linecache.getline(filename, line_num) |
| 1310 | +error_msg = traceback.format_exc() |
1308 | 1311 | </code></pre></div>
|
1309 | 1312 |
|
1310 | 1313 | <div><h3 id="commonbuiltinexceptions">Common Built-in Exceptions</h3><pre><code class="text language-text">BaseException
|
|
1329 | 1332 | +-- UnicodeError # Raised when encoding/decoding strings from/to bytes fails.
|
1330 | 1333 | </code></pre></div>
|
1331 | 1334 |
|
| 1335 | +<div><h4 id="usefulbuiltinexceptions">Useful built-in exceptions:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">raise</span> TypeError(<span class="hljs-string">'Argument is of wrong type!'</span>) |
| 1336 | +<span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">'Argument is of right type but inappropriate value!'</span>) |
| 1337 | +<span class="hljs-keyword">raise</span> RuntimeError(<span class="hljs-string">'None of above!'</span>) |
| 1338 | +</code></pre></div> |
| 1339 | + |
1332 | 1340 | <div><h4 id="collectionsandtheirexceptions">Collections and their exceptions:</h4><pre><code class="text language-text">+-----------+------------+------------+------------+
|
1333 | 1341 | | | list | dict | set |
|
1334 | 1342 | +-----------+------------+------------+------------+
|
|
1854 | 1862 |
|
1855 | 1863 |
|
1856 | 1864 |
|
1857 |
| -<div><h3 id="attributes-1">Attributes</h3><pre><code class="python language-python hljs"><list> = dir(<object>) <span class="hljs-comment"># Returns names of object's attributes (incl. methods).</span> |
| 1865 | +<div><h3 id="attributes-2">Attributes</h3><pre><code class="python language-python hljs"><list> = dir(<object>) <span class="hljs-comment"># Returns names of object's attributes (incl. methods).</span> |
1858 | 1866 | <dict> = vars(<object>) <span class="hljs-comment"># Returns dict of object's fields. Also <object>.__dict__.</span>
|
1859 | 1867 | </code></pre></div>
|
1860 | 1868 |
|
|
0 commit comments