|
56 | 56 |
|
57 | 57 | <body> |
58 | 58 | <header> |
59 | | - <aside>July 29, 2025</aside> |
| 59 | + <aside>July 30, 2025</aside> |
60 | 60 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a> |
61 | 61 | </header> |
62 | 62 |
|
|
827 | 827 | (<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>, <span class="hljs-string">'MyClass(1)'</span>) |
828 | 828 | </code></pre> |
829 | 829 | <ul> |
830 | | -<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, <code class="python hljs"><span class="hljs-string">'print(a)'</span></code> calls <code class="python hljs"><span class="hljs-string">'a.__str__()'</span></code> and <code class="python hljs"><span class="hljs-string">'a + b'</span></code> calls <code class="python hljs"><span class="hljs-string">'a.__add__(b)'</span></code>.</strong></li> |
| 830 | +<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,<br>for example, <code class="python hljs"><span class="hljs-string">'print(a)'</span></code> calls <code class="python hljs"><span class="hljs-string">'a.__str__()'</span></code> and <code class="python hljs"><span class="hljs-string">'a + b'</span></code> calls <code class="python hljs"><span class="hljs-string">'a.__add__(b)'</span></code>.</strong></li> |
831 | 831 | <li><strong>Methods decorated with <code class="python hljs"><span class="hljs-string">'@staticmethod'</span></code> receive neither 'self' nor 'cls' argument.</strong></li> |
832 | 832 | <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> |
833 | 833 | </ul> |
|
891 | 891 | <ul> |
892 | 892 | <li><strong>Objects can be made <a href="#sortable">sortable</a> with <code class="python hljs"><span class="hljs-string">'order=True'</span></code> and immutable with <code class="python hljs"><span class="hljs-string">'frozen=True'</span></code>.</strong></li> |
893 | 893 | <li><strong>For object to be <a href="#hashable">hashable</a>, all attributes must be hashable and 'frozen' must be True.</strong></li> |
894 | | -<li><strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">'<attr_name>: list = []'</span></code> would make a list that is shared among all instances. Its 'default_factory' argument can be any <a href="#callable">callable</a>.</strong></li> |
| 894 | +<li><strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">'<attr_name>: list = []'</span></code> would make a list that is shared among all instances. Its 'default_factory' argument accepts any callable object.</strong></li> |
895 | 895 | <li><strong>For attributes of arbitrary type use <code class="python hljs"><span class="hljs-string">'typing.Any'</span></code>.</strong></li> |
896 | 896 | </ul> |
897 | 897 | <pre><code class="python language-python hljs">P = make_dataclass(<span class="hljs-string">'P'</span>, [<span class="hljs-string">'x'</span>, <span class="hljs-string">'y'</span>]) |
|
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 | 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 | | -<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> |
| 930 | +<li><strong>Only the left side object has eq() method called, unless it returns NotImplemented, in which case the right object is consulted. Result is False 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> |
933 | 933 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span> |
|
1002 | 1002 | (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>) |
1003 | 1003 | </code></pre> |
1004 | 1004 | <div><h4 id="pythonhasmanydifferentiteratorobjects">Python has many different iterator objects:</h4><ul> |
1005 | | -<li><strong>Sequence iterators returned by the <a href="#iterator">iter()</a> function, such as list_iterator and set_iterator.</strong></li> |
| 1005 | +<li><strong>Sequence iterators returned by the <a href="#iterator">iter()</a> function, such as list_iterator, etc.</strong></li> |
1006 | 1006 | <li><strong>Objects returned by the <a href="#itertools">itertools</a> module, such as count, repeat and cycle.</strong></li> |
1007 | 1007 | <li><strong>Generators returned by the <a href="#generator">generator functions</a> and <a href="#comprehensions">generator expressions</a>.</strong></li> |
1008 | 1008 | <li><strong>File objects returned by the <a href="#open">open()</a> function, etc.</strong></li> |
|
1212 | 1212 | <ul> |
1213 | 1213 | <li><strong>Also catches subclasses, e.g. <code class="python hljs"><span class="hljs-string">'IndexError'</span></code> is caught by <code class="python hljs"><span class="hljs-string">'except LookupError:'</span></code>.</strong></li> |
1214 | 1214 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'traceback.print_exc()'</span></code> to print the full error message to standard error stream.</strong></li> |
1215 | | -<li><strong>Use <code class="python hljs"><span class="hljs-string">'print(<name>)'</span></code> to print just the cause of the exception (i.e. its arguments).</strong></li> |
| 1215 | +<li><strong>Use <code class="python hljs"><span class="hljs-string">'print(<name>)'</span></code> to print just the cause of the exception (that is, its arguments).</strong></li> |
1216 | 1216 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'logging.exception(<str>)'</span></code> to log the passed message, followed by the full error message of the caught exception. For details about setting up the logger see <a href="#logging">Logging</a>.</strong></li> |
1217 | 1217 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'sys.exc_info()'</span></code> to get exception type, object, and traceback of caught exception.</strong></li> |
1218 | 1218 | </ul> |
|
1327 | 1327 | <ul> |
1328 | 1328 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'help=<str>'</span></code> to set argument description that will be displayed in help message.</strong></li> |
1329 | 1329 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'default=<obj>'</span></code> to override None as option's or optional argument's default value.</strong></li> |
1330 | | -<li><strong>Use <code class="python hljs"><span class="hljs-string">'type=FileType(<mode>)'</span></code> for files. Accepts 'encoding', but 'newline' is None.</strong></li> |
| 1330 | +<li><strong>Use <code class="python hljs"><span class="hljs-string">'type=FileType(<mode>)'</span></code> for files. It accepts 'encoding', but 'newline' is None.</strong></li> |
1331 | 1331 | </ul> |
1332 | 1332 | <div><h2 id="open"><a href="#open" name="open">#</a>Open</h2><p><strong>Opens a file and returns the corresponding file object.</strong></p><pre><code class="python language-python hljs"><file> = open(<path>, mode=<span class="hljs-string">'r'</span>, encoding=<span class="hljs-keyword">None</span>, newline=<span class="hljs-keyword">None</span>) |
1333 | 1333 | </code></pre></div> |
|
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 object's path (relative by default).</span> |
| 1411 | +<str> = <DirEntry>.path <span class="hljs-comment"># Is absolute if 'path' argument was absolute.</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> |
|
1539 | 1539 | <li><strong><code class="python hljs"><span class="hljs-string">'lineterminator'</span></code> - How writer terminates rows. Reader looks for '\n', '\r' and '\r\n'.</strong></li> |
1540 | 1540 | <li><strong><code class="python hljs"><span class="hljs-string">'quotechar'</span></code> - Character for quoting fields containing delimiters, quotechars, '\n' or '\r'.</strong></li> |
1541 | 1541 | <li><strong><code class="python hljs"><span class="hljs-string">'escapechar'</span></code> - Character for escaping quotechars (not needed if doublequote is True).</strong></li> |
1542 | | -<li><strong><code class="python hljs"><span class="hljs-string">'doublequote'</span></code> - Whether quotechars inside fields are (or get) doubled or escaped.</strong></li> |
| 1542 | +<li><strong><code class="python hljs"><span class="hljs-string">'doublequote'</span></code> - Whether quotechars inside fields are/get doubled instead of escaped.</strong></li> |
1543 | 1543 | <li><strong><code class="python hljs"><span class="hljs-string">'quoting'</span></code> - 0: As necessary, 1: All, 2: All but numbers which are read as floats, 3: None.</strong></li> |
1544 | 1544 | <li><strong><code class="python hljs"><span class="hljs-string">'skipinitialspace'</span></code> - Is space character at the start of the field stripped by the reader.</strong></li> |
1545 | 1545 | </ul><div><h3 id="dialects">Dialects</h3><pre><code class="text language-text">┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓ |
@@ -1692,7 +1692,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment |
1692 | 1692 |
|
1693 | 1693 |
|
1694 | 1694 |
|
1695 | | -<div><h2 id="array"><a href="#array" name="array">#</a>Array</h2><p><strong>List that can only hold numbers of a predefined C type. Available types and their minimum sizes in bytes are listed above. Type sizes and byte order are always determined by the system, however bytes of each element can be reversed with byteswap() method.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> array <span class="hljs-keyword">import</span> array |
| 1695 | +<div><h2 id="array"><a href="#array" name="array">#</a>Array</h2><p><strong>List that can only hold numbers of a predefined type. Available types and their minimum sizes in bytes are listed above. Type sizes and byte order are always determined by the system, however bytes of each element can be reversed with byteswap() method.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> array <span class="hljs-keyword">import</span> array |
1696 | 1696 | </code></pre></div> |
1697 | 1697 |
|
1698 | 1698 |
|
@@ -1727,7 +1727,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment |
1727 | 1727 |
|
1728 | 1728 | <pre><code class="python language-python hljs"><deque> = deque(<collection>) <span class="hljs-comment"># Use `maxlen=<int>` to set size limit.</span> |
1729 | 1729 | <deque>.appendleft(<el>) <span class="hljs-comment"># Opposite element is dropped if full.</span> |
1730 | | -<deque>.extendleft(<collection>) <span class="hljs-comment"># Appends elements in reversed order.</span> |
| 1730 | +<deque>.extendleft(<collection>) <span class="hljs-comment"># Prepends reversed coll. to the deque.</span> |
1731 | 1731 | <deque>.rotate(n=<span class="hljs-number">1</span>) <span class="hljs-comment"># Last element becomes the first one.</span> |
1732 | 1732 | <el> = <deque>.popleft() <span class="hljs-comment"># Raises IndexError if deque is empty.</span> |
1733 | 1733 | </code></pre> |
@@ -2934,7 +2934,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment |
2934 | 2934 |
|
2935 | 2935 |
|
2936 | 2936 | <footer> |
2937 | | - <aside>July 29, 2025</aside> |
| 2937 | + <aside>July 30, 2025</aside> |
2938 | 2938 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a> |
2939 | 2939 | </footer> |
2940 | 2940 |
|
|
0 commit comments