|
926 | 926 | </code></pre></div> |
927 | 927 |
|
928 | 928 | <div><h2 id="ducktypes"><a href="#ducktypes" name="ducktypes">#</a>Duck Types</h2><p><strong>A duck type is an implicit type that prescribes a set of special methods. Any object that has those methods defined is considered a member of that duck type.</strong></p><div><h3 id="comparable">Comparable</h3><ul> |
929 | | -<li><strong>If eq() method is not overridden, it returns <code class="python hljs"><span class="hljs-string">'id(self) == id(other)'</span></code>, which is the same as <code class="python hljs"><span class="hljs-string">'self is other'</span></code>. That means all user-defined objects compare not equal by default (because id() returns object's memory address that is unique during its lifetime).</strong></li> |
| 929 | +<li><strong>If eq() method is not overridden, it returns <code class="python hljs"><span class="hljs-string">'id(self) == id(other)'</span></code>, which is the same as <code class="python hljs"><span class="hljs-string">'self is other'</span></code>. That means all user-defined objects compare not equal by default (because id() returns object's memory address that is guaranteed to be unique).</strong></li> |
930 | 930 | <li><strong>Only the left side object has eq() method called, unless it returns NotImplemented, in which case the right object is consulted. False is returned if both return NotImplemented.</strong></li> |
931 | 931 | <li><strong>Ne() automatically works on any object that has eq() defined.</strong></li> |
932 | 932 | </ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyComparable</span>:</span> |
|
1365 | 1365 | <pre><code class="python language-python hljs"><str/bytes> = <file>.read(size=<span class="hljs-number">-1</span>) <span class="hljs-comment"># Reads 'size' chars/bytes or until the EOF.</span> |
1366 | 1366 | <str/bytes> = <file>.readline() <span class="hljs-comment"># Returns a line or empty string/bytes on EOF.</span> |
1367 | 1367 | <list> = <file>.readlines() <span class="hljs-comment"># Returns remaining lines. Also list(<file>).</span> |
1368 | | -<str/bytes> = next(<file>) <span class="hljs-comment"># Uses read-ahead buffer. Don't mix with above.</span> |
| 1368 | +<str/bytes> = next(<file>) <span class="hljs-comment"># Returns a line using a read-ahead buffer.</span> |
1369 | 1369 | </code></pre> |
1370 | 1370 | <pre><code class="python language-python hljs"><file>.write(<str/bytes>) <span class="hljs-comment"># Writes a str or bytes object to write buffer.</span> |
1371 | 1371 | <file>.writelines(<collection>) <span class="hljs-comment"># Writes a coll. of strings or bytes objects.</span> |
|
1408 | 1408 | <num> = <stat>.st_mtime/st_size/… <span class="hljs-comment"># Returns modification time, size in bytes, etc.</span> |
1409 | 1409 | </code></pre> |
1410 | 1410 | <div><h3 id="direntry">DirEntry</h3><p><strong>Unlike listdir(), scandir() returns DirEntry objects that cache isfile, isdir, and on Windows also stat information, thus significantly increasing the performance of code that requires it.</strong></p><pre><code class="python language-python hljs"><iter> = os.scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns DirEntry objects located at the path.</span> |
1411 | | -<str> = <DirEntry>.path <span class="hljs-comment"># Returns the whole path (relative by default).</span> |
| 1411 | +<str> = <DirEntry>.path <span class="hljs-comment"># Returns object's path (relative by default).</span> |
1412 | 1412 | <str> = <DirEntry>.name <span class="hljs-comment"># Returns path's final component as a string.</span> |
1413 | 1413 | <file> = open(<DirEntry>) <span class="hljs-comment"># Opens the file and returns its file object.</span> |
1414 | 1414 | </code></pre></div> |
|
0 commit comments