mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
syntax highlighting for everyone!
This commit is contained in:
+132
-134
@@ -23,11 +23,11 @@ td a:link, td a:visited{border:0}
|
||||
<meta name=viewport content='initial-scale=1.0'>
|
||||
</head>
|
||||
<form action=http://www.google.com/cse><div><input type=hidden name=cx value=014021643941856155761:l5eihuescdw><input type=hidden name=ie value=UTF-8> <input name=q size=25> <input type=submit name=sa value=Search></div></form>
|
||||
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#special-method-names>Dive Into Python 3</a> <span>‣</span>
|
||||
<p>You are here: <a href=index.html>Home</a> <span class=u>‣</span> <a href=table-of-contents.html#special-method-names>Dive Into Python 3</a> <span class=u>‣</span>
|
||||
<p id=level>Difficulty level: <span title=pro>♦♦♦♦♦</span>
|
||||
<h1>Special Method Names</h1>
|
||||
<blockquote class=q>
|
||||
<p><span>❝</span> My specialty is being right when other people are wrong. <span>❞</span><br>— <a href=http://en.wikiquote.org/wiki/George_Bernard_Shaw>George Bernard Shaw</a>
|
||||
<p><span class=u>❝</span> My specialty is being right when other people are wrong. <span class=u>❞</span><br>— <a href=http://en.wikiquote.org/wiki/George_Bernard_Shaw>George Bernard Shaw</a>
|
||||
</blockquote>
|
||||
<p id=toc>
|
||||
<h2 id=divingin>Diving in</h2>
|
||||
@@ -44,24 +44,24 @@ td a:link, td a:visited{border:0}
|
||||
<th>And Python Calls…
|
||||
<tr><th>①
|
||||
<td>to initialize an instance
|
||||
<td><code>x = MyClass()</code>
|
||||
<td><code>x.__init__()</code>
|
||||
<td><code class=pp>x = MyClass()</code>
|
||||
<td><code class=pp>x.__init__()</code>
|
||||
<tr><th>②
|
||||
<td>the “official” representation as a string
|
||||
<td><code>repr(x)</code>
|
||||
<td><code>x.__repr__()</code>
|
||||
<td><code class=pp>repr(x)</code>
|
||||
<td><code class=pp>x.__repr__()</code>
|
||||
<tr><th>③
|
||||
<td>the “informal” value as a string
|
||||
<td><code>str(x)</code>
|
||||
<td><code>x.__str__()</code>
|
||||
<td><code class=pp>str(x)</code>
|
||||
<td><code class=pp>x.__str__()</code>
|
||||
<tr><th>④
|
||||
<td>the “informal” value as a byte array
|
||||
<td><code>bytes(x)</code>
|
||||
<td><code>x.__bytes__()</code>
|
||||
<td><code class=pp>bytes(x)</code>
|
||||
<td><code class=pp>x.__bytes__()</code>
|
||||
<tr><th>⑤
|
||||
<td>the value as a formatted string
|
||||
<td><code>format(x)</code>
|
||||
<td><code>x.__format__(<var>format_spec</var>)</code>
|
||||
<td><code class=pp>format(x)</code>
|
||||
<td><code class=pp>x.__format__(<var>format_spec</var>)</code>
|
||||
</table>
|
||||
<ol>
|
||||
<li>The <code>__init__()</code> method is called <em>after</em> the instance is created. If you want to control the actual creation process, use <a href=#esoterica>the <code>__new__()</code> method</a>.
|
||||
@@ -82,15 +82,15 @@ td a:link, td a:visited{border:0}
|
||||
<th>And Python Calls…
|
||||
<tr><th>①
|
||||
<td>to iterate through a sequence
|
||||
<td><code>iter(seq)</code>
|
||||
<td><code class=pp>iter(seq)</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__iter__><code>seq.__iter__()</code></a>
|
||||
<tr><th>②
|
||||
<td>to get the next value from an iterator
|
||||
<td><code>next(seq)</code>
|
||||
<td><code class=pp>next(seq)</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__next__><code>seq.__next__()</code></a>
|
||||
<tr><th>③
|
||||
<td>to create an iterator in reverse order
|
||||
<td><code>reversed(seq)</code>
|
||||
<td><code class=pp>reversed(seq)</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__reversed__><code>seq.__reversed__()</code></a>
|
||||
</table>
|
||||
<ol>
|
||||
@@ -110,23 +110,23 @@ td a:link, td a:visited{border:0}
|
||||
<th>And Python Calls…
|
||||
<tr><th>①
|
||||
<td>to get a computed attribute (unconditionally)
|
||||
<td><code>x.my_property</code>
|
||||
<td><code class=pp>x.my_property</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__getattribute__><code>x.__getattribute__(<var>'my_property'</var>)</code></a>
|
||||
<tr><th>②
|
||||
<td>to get a computed attribute (fallback)
|
||||
<td><code>x.my_property</code>
|
||||
<td><code class=pp>x.my_property</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__getattr__><code>x.__getattr__(<var>'my_property'</var>)</code></a>
|
||||
<tr><th>③
|
||||
<td>to set an attribute
|
||||
<td><code>x.my_property = value</code>
|
||||
<td><code class=pp>x.my_property = value</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__setattr__><code>x.__setattr__(<var>'my_property'</var>, <var>value</var>)</code></a>
|
||||
<tr><th>④
|
||||
<td>to delete an attribute
|
||||
<td><code>del x.my_property</code>
|
||||
<td><code class=pp>del x.my_property</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__delattr__><code>x.__delattr__(<var>'my_property'</var>)</code></a>
|
||||
<tr><th>⑤
|
||||
<td>to list all attributes and methods
|
||||
<td><code>dir(x)</code>
|
||||
<td><code class=pp>dir(x)</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__dir__><code>x.__dir__()</code></a>
|
||||
</table>
|
||||
<ol>
|
||||
@@ -142,16 +142,16 @@ td a:link, td a:visited{border:0}
|
||||
<pre class=screen>
|
||||
<code>class Dynamo:
|
||||
def __getattr__(self, key):
|
||||
<a> if key == 'color': <span>①</span></a>
|
||||
<a> if key == 'color': <span class=u>①</span></a>
|
||||
return 'PapayaWhip'
|
||||
else:
|
||||
<a> raise AttributeError <span>②</span></a></code>
|
||||
<a> raise AttributeError <span class=u>②</span></a></code>
|
||||
|
||||
<samp class=p>>>> </samp><kbd>dyn = Dynamo()</kbd>
|
||||
<a><samp class=p>>>> </samp><kbd>dyn.color</kbd> <span>③</span></a>
|
||||
<a><samp class=p>>>> </samp><kbd>dyn.color</kbd> <span class=u>③</span></a>
|
||||
<samp>'PapayaWhip'</samp>
|
||||
<samp class=p>>>> </samp><kbd>dyn.color = 'LemonChiffon'</kbd>
|
||||
<a><samp class=p>>>> </samp><kbd>dyn.color</kbd> <span>④</span></a>
|
||||
<a><samp class=p>>>> </samp><kbd>dyn.color</kbd> <span class=u>④</span></a>
|
||||
<samp>'LemonChiffon'</samp></pre>
|
||||
<ol>
|
||||
<li>The attribute name is passed into the <code>__getattr()__</code> method as a string. If the name is <code>'color'</code>, the method returns a value. (In this case, it’s just a hard-coded string, but you would normally do some sort of computation and return the result.)
|
||||
@@ -171,10 +171,10 @@ td a:link, td a:visited{border:0}
|
||||
raise AttributeError</code>
|
||||
|
||||
<samp class=p>>>> </samp><kbd>dyn = SuperDynamo()</kbd>
|
||||
<a><samp class=p>>>> </samp><kbd>dyn.color</kbd> <span>①</span></a>
|
||||
<a><samp class=p>>>> </samp><kbd>dyn.color</kbd> <span class=u>①</span></a>
|
||||
<samp>'PapayaWhip'</samp>
|
||||
<samp class=p>>>> </samp><kbd>dyn.color = 'LemonChiffon'</kbd>
|
||||
<a><samp class=p>>>> </samp><kbd>dyn.color</kbd> <span>②</span></a>
|
||||
<a><samp class=p>>>> </samp><kbd>dyn.color</kbd> <span class=u>②</span></a>
|
||||
<samp>'PapayaWhip'</samp></pre>
|
||||
<ol>
|
||||
<li>The <code>__getattribute__()</code> method is called to provide a value for <var>dyn.color</var>.
|
||||
@@ -182,7 +182,7 @@ td a:link, td a:visited{border:0}
|
||||
</ol>
|
||||
|
||||
<blockquote class=note>
|
||||
<p><span>☞</span>If your class defines a <code>__getattribute__()</code> method, you probably also want to define a <code>__setattr__()</code> method and coordinate between them to keep track of attribute values. Otherwise, any attributes you set after creating an instance will disappear into a black hole.
|
||||
<p><span class=u>☞</span>If your class defines a <code>__getattribute__()</code> method, you probably also want to define a <code>__setattr__()</code> method and coordinate between them to keep track of attribute values. Otherwise, any attributes you set after creating an instance will disappear into a black hole.
|
||||
</blockquote>
|
||||
|
||||
<p>You need to be extra careful with the <code>__getattribute__()</code> method, because it is also called when Python looks up a method name on your class.
|
||||
@@ -190,12 +190,12 @@ td a:link, td a:visited{border:0}
|
||||
<pre class=screen>
|
||||
<code>class Rastan:
|
||||
def __getattribute__(self, key):
|
||||
<a> raise AttributeError <span>①</span></a>
|
||||
<a> raise AttributeError <span class=u>①</span></a>
|
||||
def swim(self):
|
||||
pass</code>
|
||||
|
||||
<samp class=p>>>> </samp><kbd>hero = Rastan()</kbd>
|
||||
<a><samp class=p>>>> </samp><kbd>hero.swim()</kbd> <span>②</span></a>
|
||||
<a><samp class=p>>>> </samp><kbd>hero.swim()</kbd> <span class=u>②</span></a>
|
||||
<samp class=traceback>Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
File "<stdin>", line 3, in __getattribute__
|
||||
@@ -216,26 +216,25 @@ AttributeError</samp></pre>
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>to “call” an instance like a function
|
||||
<td><code>my_instance()</code>
|
||||
<td><code class=pp>my_instance()</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__call__><code>my_instance.__call__()</code></a>
|
||||
</table>
|
||||
|
||||
<p>The <a href=http://docs.python.org/3.0/library/zipfile.html><code>zipfile</code> module</a> uses this to define a class that can decrypt an encrypted zip file with a given password. The zip decryption algorithm requires you to store state during decryption. Defining the decryptor as a class allows you to maintain this state within a single instance of the decryptor class. The state is initialized in the <code>__init__()</code> method and updated as the file is decrypted. But since the class is also “callable” like a function, you can pass the instance as the first argument of the <code>map()</code> function, like so:
|
||||
|
||||
<pre><code>
|
||||
# excerpt from zipfile.py
|
||||
<pre><code class=pp># excerpt from zipfile.py
|
||||
class _ZipDecrypter:
|
||||
.
|
||||
.
|
||||
.
|
||||
def __init__(self, pwd):
|
||||
<a> self.key0 = 305419896 <span>①</span></a>
|
||||
<a> self.key0 = 305419896 <span class=u>①</span></a>
|
||||
self.key1 = 591751049
|
||||
self.key2 = 878082192
|
||||
for p in pwd:
|
||||
self._UpdateKeys(p)
|
||||
|
||||
<a> def __call__(self, c): <span>②</span></a>
|
||||
<a> def __call__(self, c): <span class=u>②</span></a>
|
||||
assert isinstance(c, int)
|
||||
k = self.key2 | 2
|
||||
c = c ^ (((k * (k^1)) >> 8) & 255)
|
||||
@@ -244,9 +243,9 @@ class _ZipDecrypter:
|
||||
.
|
||||
.
|
||||
.
|
||||
<a>zd = _ZipDecrypter(pwd) <span>③</span></a>
|
||||
<a>zd = _ZipDecrypter(pwd) <span class=u>③</span></a>
|
||||
bytes = zef_file.read(12)
|
||||
<a>h = list(map(zd, bytes[0:12])) <span>④</span></a></code></pre>
|
||||
<a>h = list(map(zd, bytes[0:12])) <span class=u>④</span></a></code></pre>
|
||||
<ol>
|
||||
<li>The <code>_ZipDecryptor</code> class maintains state in the form of three rotating keys, which are later updated in the <code>_UpdateKeys()</code> method (not shown here).
|
||||
<li>The class defines a <code>__call__()</code> method, which makes class instances callable like functions. In this case, the <code>__call__()</code> method decrypts a single byte of the zip file, then updates the rotating keys based on the byte that was decrypted.
|
||||
@@ -265,21 +264,20 @@ bytes = zef_file.read(12)
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>the length of a sequence
|
||||
<td><code>len(seq)</code>
|
||||
<td><code class=pp>len(seq)</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__len__><code>seq.__len__()</code></a>
|
||||
<tr><th>
|
||||
<td>to know whether a sequence contains a specific value
|
||||
<td><code>x in seq</code>
|
||||
<td><code class=pp>x in seq</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__contains__><code>seq.__contains__(<var>x</var>)</code></a>
|
||||
</table>
|
||||
|
||||
<p id=acts-like-list-example>The <a href=http://docs.python.org/3.0/library/cgi.html><code>cgi</code> module</a> uses these methods in its <code>FieldStorage</code> class, which represents all of the form fields or query parameters submitted to a dynamic web page.
|
||||
|
||||
<pre><code>
|
||||
# A script which responds to http://example.com/search?q=cgi
|
||||
<pre><code class=pp># A script which responds to http://example.com/search?q=cgi
|
||||
import cgi
|
||||
fs = cgi.FieldStorage()
|
||||
<a>if 'q' in fs: <span>①</span></a>
|
||||
<a>if 'q' in fs: <span class=u>①</span></a>
|
||||
do_search()
|
||||
|
||||
# An excerpt from cgi.py that explains how that works
|
||||
@@ -287,12 +285,12 @@ class FieldStorage:
|
||||
.
|
||||
.
|
||||
.
|
||||
<a> def __contains__(self, key): <span>②</span></a>
|
||||
<a> def __contains__(self, key): <span class=u>②</span></a>
|
||||
if self.list is None:
|
||||
raise TypeError('not indexable')
|
||||
<a> return any(item.name == key for item in self.list) <span>③</span></a>
|
||||
<a> return any(item.name == key for item in self.list) <span class=u>③</span></a>
|
||||
|
||||
<a> def __len__(self): <span>④</span></a>
|
||||
<a> def __len__(self): <span class=u>④</span></a>
|
||||
return len(self.keys())</code></pre>
|
||||
<ol>
|
||||
<li>Once you create an instance of the <code>cgi.FieldStorage</code> class, you can use the “<code>in</code>” operator to check whether a particular parameter was included in the query string.
|
||||
@@ -312,37 +310,36 @@ class FieldStorage:
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>to get a value by its key
|
||||
<td><code>x[key]</code>
|
||||
<td><code class=pp>x[key]</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__getitem__><code>x.__getitem__(<var>'key'</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>to set a value by its key
|
||||
<td><code>x[key] = value</code>
|
||||
<td><code class=pp>x[key] = value</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__setitem__><code>x.__setitem__(<var>'key'</var>, <var>value</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>to delete a key-value pair
|
||||
<td><code>del x[key]</code>
|
||||
<td><code class=pp>del x[key]</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__delitem__><code>x.__delitem__(<var>'key'</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>to provide a default value for missing keys
|
||||
<td><code>x[nonexistent_key]</code>
|
||||
<td><code class=pp>x[nonexistent_key]</code>
|
||||
<td><a href=http://docs.python.org/3.0/library/collections.html#collections.defaultdict.__missing__><code>x.__missing__(<var>'nonexistent_key'</var>)</code></a>
|
||||
</table>
|
||||
|
||||
<p>The <a href=#acts-like-list-example><code>FieldStorage</code> class</a> from the <a href=http://docs.python.org/3.0/library/cgi.html><code>cgi</code> module</a> also defines these special methods, which means you can do things like this:
|
||||
|
||||
<pre><code>
|
||||
# A script which responds to http://example.com/search?q=cgi
|
||||
<pre><code class=pp># A script which responds to http://example.com/search?q=cgi
|
||||
import cgi
|
||||
fs = cgi.FieldStorage()
|
||||
if 'q' in fs:
|
||||
<a> do_search(fs['q']) <span>①</span></a>
|
||||
<a> do_search(fs['q']) <span class=u>①</span></a>
|
||||
|
||||
# An excerpt from cgi.py that shows how it works
|
||||
class FieldStorage:
|
||||
.
|
||||
.
|
||||
.
|
||||
<a> def __getitem__(self, key): <span>②</span></a>
|
||||
<a> def __getitem__(self, key): <span class=u>②</span></a>
|
||||
if self.list is None:
|
||||
raise TypeError('not indexable')
|
||||
found = []
|
||||
@@ -378,55 +375,55 @@ class FieldStorage:
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>addition
|
||||
<td><code>x + y</code>
|
||||
<td><code class=pp>x + y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__add__><code>x.__add__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>subtraction
|
||||
<td><code>x - y</code>
|
||||
<td><code class=pp>x - y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__sub__><code>x.__sub__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>multiplication
|
||||
<td><code>x * y</code>
|
||||
<td><code class=pp>x * y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__mul__><code>x.__mul__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>division
|
||||
<td><code>x / y</code>
|
||||
<td><code class=pp>x / y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__truediv__><code>x.__truediv__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>floor division
|
||||
<td><code>x // y</code>
|
||||
<td><code class=pp>x // y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__floordiv__><code>x.__floordiv__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>modulo (remainder)
|
||||
<td><code>x % y</code>
|
||||
<td><code class=pp>x % y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__mod__><code>x.__mod__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>floor division <i class=baa>&</i> modulo
|
||||
<td><code>divmod(x, y)</code>
|
||||
<td><code class=pp>divmod(x, y)</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__divmod__><code>x.__divmod__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>raise to power
|
||||
<td><code>x ** y</code>
|
||||
<td><code class=pp>x ** y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__pow__><code>x.__pow__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>left bit-shift
|
||||
<td><code>x << y</code>
|
||||
<td><code class=pp>x << y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__lshift__><code>x.__lshift__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>right bit-shift
|
||||
<td><code>x >> y</code>
|
||||
<td><code class=pp>x >> y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__rshift__><code>x.__rshift__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>bitwise <code>and</code>
|
||||
<td><code>x & y</code>
|
||||
<td><code class=pp>x & y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__and__><code>x.__and__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>bitwise <code>xor</code>
|
||||
<td><code>x ^ y</code>
|
||||
<td><code class=pp>x ^ y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__xor__><code>x.__xor__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>bitwise <code>or</code>
|
||||
<td><code>x | y</code>
|
||||
<td><code class=pp>x | y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__or__><code>x.__or__(<var>y</var>)</code></a>
|
||||
</table>
|
||||
|
||||
@@ -456,55 +453,55 @@ class FieldStorage:
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>addition
|
||||
<td><code>x + y</code>
|
||||
<td><code class=pp>x + y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__radd__><code>y.__radd__(<var>x</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>subtraction
|
||||
<td><code>x - y</code>
|
||||
<td><code class=pp>x - y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__rsub__><code>y.__rsub__(<var>x</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>multiplication
|
||||
<td><code>x * y</code>
|
||||
<td><code class=pp>x * y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__rmul__><code>y.__rmul__(<var>x</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>division
|
||||
<td><code>x / y</code>
|
||||
<td><code class=pp>x / y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__rtruediv__><code>y.__rtruediv__(<var>x</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>floor division
|
||||
<td><code>x // y</code>
|
||||
<td><code class=pp>x // y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__rfloordiv__><code>y.__rfloordiv__(<var>x</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>modulo (remainder)
|
||||
<td><code>x % y</code>
|
||||
<td><code class=pp>x % y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__rmod__><code>y.__rmod__(<var>x</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>floor division <i class=baa>&</i> modulo
|
||||
<td><code>divmod(x, y)</code>
|
||||
<td><code class=pp>divmod(x, y)</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__rdivmod__><code>y.__rdivmod__(<var>x</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>raise to power
|
||||
<td><code>x ** y</code>
|
||||
<td><code class=pp>x ** y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__rpow__><code>y.__rpow__(<var>x</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>left bit-shift
|
||||
<td><code>x << y</code>
|
||||
<td><code class=pp>x << y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__rlshift__><code>y.__rlshift__(<var>x</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>right bit-shift
|
||||
<td><code>x >> y</code>
|
||||
<td><code class=pp>x >> y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__rrshift__><code>y.__rrshift__(<var>x</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>bitwise <code>and</code>
|
||||
<td><code>x & y</code>
|
||||
<td><code class=pp>x & y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__rand__><code>y.__rand__(<var>x</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>bitwise <code>xor</code>
|
||||
<td><code>x ^ y</code>
|
||||
<td><code class=pp>x ^ y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__rxor__><code>y.__rxor__(<var>x</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>bitwise <code>or</code>
|
||||
<td><code>x | y</code>
|
||||
<td><code class=pp>x | y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__ror__><code>y.__ror__(<var>x</var>)</code></a>
|
||||
</table>
|
||||
|
||||
@@ -517,51 +514,51 @@ class FieldStorage:
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>in-place addition
|
||||
<td><code>x += y</code>
|
||||
<td><code class=pp>x += y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__iadd__><code>x.__iadd__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>in-place subtraction
|
||||
<td><code>x -= y</code>
|
||||
<td><code class=pp>x -= y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__isub__><code>x.__isub__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>in-place multiplication
|
||||
<td><code>x *= y</code>
|
||||
<td><code class=pp>x *= y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__imul__><code>x.__imul__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>in-place division
|
||||
<td><code>x /= y</code>
|
||||
<td><code class=pp>x /= y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__itruediv__><code>x.__itruediv__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>in-place floor division
|
||||
<td><code>x //= y</code>
|
||||
<td><code class=pp>x //= y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__ifloordiv__><code>x.__ifloordiv__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>in-place modulo
|
||||
<td><code>x %= y</code>
|
||||
<td><code class=pp>x %= y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__imod__><code>x.__imod__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>in-place raise to power
|
||||
<td><code>x **= y</code>
|
||||
<td><code class=pp>x **= y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__ipow__><code>x.__ipow__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>in-place left bit-shift
|
||||
<td><code>x <<= y</code>
|
||||
<td><code class=pp>x <<= y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__ilshift__><code>x.__ilshift__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>in-place right bit-shift
|
||||
<td><code>x >>= y</code>
|
||||
<td><code class=pp>x >>= y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__irshift__><code>x.__irshift__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>in-place bitwise <code>and</code>
|
||||
<td><code>x &= y</code>
|
||||
<td><code class=pp>x &= y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__iand__><code>x.__iand__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>in-place bitwise <code>xor</code>
|
||||
<td><code>x ^= y</code>
|
||||
<td><code class=pp>x ^= y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__ixor__><code>x.__ixor__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>in-place bitwise <code>or</code>
|
||||
<td><code>x |= y</code>
|
||||
<td><code class=pp>x |= y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__ior__><code>x.__ior__(<var>y</var>)</code></a>
|
||||
</table>
|
||||
|
||||
@@ -584,55 +581,55 @@ class FieldStorage:
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>negative number
|
||||
<td><code>-x</code>
|
||||
<td><code class=pp>-x</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__neg__><code>x.__neg__()</code></a>
|
||||
<tr><th>
|
||||
<td>positive number
|
||||
<td><code>+x</code>
|
||||
<td><code class=pp>+x</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__pos__><code>x.__pos__()</code></a>
|
||||
<tr><th>
|
||||
<td>absolute value
|
||||
<td><code>abs(x)</code>
|
||||
<td><code class=pp>abs(x)</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__abs__><code>x.__abs__()</code></a>
|
||||
<tr><th>
|
||||
<td>inverse
|
||||
<td><code>~x</code>
|
||||
<td><code class=pp>~x</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__invert__><code>x.__invert__()</code></a>
|
||||
<tr><th>
|
||||
<td>complex number
|
||||
<td><code>complex(x)</code>
|
||||
<td><code class=pp>complex(x)</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__complex__><code>x.__complex__()</code></a>
|
||||
<tr><th>
|
||||
<td>integer
|
||||
<td><code>int(x)</code>
|
||||
<td><code class=pp>int(x)</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__int__><code>x.__int__()</code></a>
|
||||
<tr><th>
|
||||
<td>floating point number
|
||||
<td><code>float(x)</code>
|
||||
<td><code class=pp>float(x)</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__float__><code>x.__float__()</code></a>
|
||||
<tr><th>
|
||||
<td>number rounded to nearest integer
|
||||
<td><code>round(x)</code>
|
||||
<td><code class=pp>round(x)</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__round__><code>x.__round__()</code></a>
|
||||
<tr><th>
|
||||
<td>number rounded to nearest <var>n</var> digits
|
||||
<td><code>round(x, n)</code>
|
||||
<td><code class=pp>round(x, n)</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__round__><code>x.__round__(n)</code></a>
|
||||
<tr><th>
|
||||
<td>smallest integer <code>>= x</code>
|
||||
<td><code>math.ceil(x)</code>
|
||||
<td><code class=pp>math.ceil(x)</code>
|
||||
<td><a href=http://docs.python.org/3.0/library/math.html#math.ceil><code>x.__ceil__()</code></a>
|
||||
<tr><th>
|
||||
<td>largest integer <code><= x</code>
|
||||
<td><code>math.floor(x)</code>
|
||||
<td><code class=pp>math.floor(x)</code>
|
||||
<td><a href=http://docs.python.org/3.0/library/math.html#math.floor><code>x.__floor__()</code></a>
|
||||
<tr><th>
|
||||
<td>truncate <code>x</code> to nearest integer toward <code>0</code>
|
||||
<td><code>math.trunc(x)</code>
|
||||
<td><code class=pp>math.trunc(x)</code>
|
||||
<td><a href=http://docs.python.org/3.0/library/math.html#math.trunc><code>x.__trunc__()</code></a>
|
||||
<tr><th><a href=http://www.python.org/dev/peps/pep-0357/>PEP 357</a>
|
||||
<td>number as a list index
|
||||
<td><code>a_list[<var>x</var>]</code>
|
||||
<td><code class=pp>a_list[<var>x</var>]</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__index__><code>x.__index__()</code></a>
|
||||
</table>
|
||||
|
||||
@@ -647,31 +644,31 @@ class FieldStorage:
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>equality
|
||||
<td><code>x == y</code>
|
||||
<td><code class=pp>x == y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__eq__><code>x.__eq__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>inequality
|
||||
<td><code>x != y</code>
|
||||
<td><code class=pp>x != y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__ne__><code>x.__ne__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>less than
|
||||
<td><code>x < y</code>
|
||||
<td><code class=pp>x < y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__lt__><code>x.__lt__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>less than or equal to
|
||||
<td><code>x <= y</code>
|
||||
<td><code class=pp>x <= y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__le__><code>x.__le__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>greater than
|
||||
<td><code>x > y</code>
|
||||
<td><code class=pp>x > y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__gt__><code>x.__gt__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>greater than or equal to
|
||||
<td><code>x >= y</code>
|
||||
<td><code class=pp>x >= y</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__ge__><code>x.__ge__(<var>y</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>truth value in a boolean context
|
||||
<td><code>if x:</code>
|
||||
<td><code class=pp>if x:</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__bool__><code>x.__bool__()</code></a>
|
||||
</table>
|
||||
|
||||
@@ -687,31 +684,31 @@ class FieldStorage:
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>a custom object copy
|
||||
<td><code>copy.copy(x)</code>
|
||||
<td><code class=pp>copy.copy(x)</code>
|
||||
<td><a href=http://docs.python.org/3.0/library/copy.html><code>x.__copy__()</code></a>
|
||||
<tr><th>
|
||||
<td>a custom object deepcopy
|
||||
<td><code>copy.deepcopy(x)</code>
|
||||
<td><code class=pp>copy.deepcopy(x)</code>
|
||||
<td><a href=http://docs.python.org/3.0/library/copy.html><code>x.__deepcopy__()</code></a>
|
||||
<tr><th>
|
||||
<td>to get an object’s state before pickling
|
||||
<td><code>pickle.dump(x, <var>file</var>)</code>
|
||||
<td><code class=pp>pickle.dump(x, <var>file</var>)</code>
|
||||
<td><a href=http://docs.python.org/3.0/library/pickle.html#pickle-state><code>x.__getstate__()</code></a>
|
||||
<tr><th>
|
||||
<td>to serialize an object
|
||||
<td><code>pickle.dump(x, <var>file</var>)</code>
|
||||
<td><code class=pp>pickle.dump(x, <var>file</var>)</code>
|
||||
<td><a href=http://docs.python.org/3.0/library/pickle.html#pickling-class-instances><code>x.__reduce__()</code></a>
|
||||
<tr><th>
|
||||
<td>to serialize an object (new pickling protocol)
|
||||
<td><code>pickle.dump(x, <var>file</var>, <var>protocol_version</var>)</code>
|
||||
<td><code class=pp>pickle.dump(x, <var>file</var>, <var>protocol_version</var>)</code>
|
||||
<td><a href=http://docs.python.org/3.0/library/pickle.html#pickling-class-instances><code>x.__reduce_ex__(<var>protocol_version</var>)</code></a>
|
||||
<tr><th>
|
||||
<td>control over how an object is created during unpickling
|
||||
<td><code>x = pickle.load(<var>file</var>)</code>
|
||||
<td><code class=pp>x = pickle.load(<var>file</var>)</code>
|
||||
<td><a href=http://docs.python.org/3.0/library/pickle.html#pickling-class-instances><code>x.__getnewargs__()</code></a>
|
||||
<tr><th>
|
||||
<td>to restore an object’s state after unpickling
|
||||
<td><code>x = pickle.load(<var>file</var>)</code>
|
||||
<td><code class=pp>x = pickle.load(<var>file</var>)</code>
|
||||
<td><a href=http://docs.python.org/3.0/library/pickle.html#pickle-state><code>x.__setstate__()</code></a>
|
||||
</table>
|
||||
|
||||
@@ -728,11 +725,11 @@ class FieldStorage:
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>do something special when entering a <code>with</code> block
|
||||
<td><code>with x:</code>
|
||||
<td><code class=pp>with x:</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__enter__><code>x.__enter__()</code></a>
|
||||
<tr><th>
|
||||
<td>do something special when leaving a <code>with</code> block
|
||||
<td><code>with x:</code>
|
||||
<td><code class=pp>with x:</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__exit__><code>x.__exit__()</code></a>
|
||||
</table>
|
||||
|
||||
@@ -740,7 +737,7 @@ class FieldStorage:
|
||||
|
||||
<p>FIXME-xref to as-yet-unwritten section on function annotations
|
||||
|
||||
<pre><code># excerpt from io.py:
|
||||
<pre><code class=pp># excerpt from io.py:
|
||||
def _checkClosed(self, msg=None):
|
||||
'''Internal: raise an ValueError if file is closed
|
||||
'''
|
||||
@@ -750,12 +747,12 @@ def _checkClosed(self, msg=None):
|
||||
|
||||
def __enter__(self) -> 'IOBase':
|
||||
'''Context management protocol. Returns self.'''
|
||||
<a> self._checkClosed() <span>①</span></a>
|
||||
<a> return self <span>②</span></a>
|
||||
<a> self._checkClosed() <span class=u>①</span></a>
|
||||
<a> return self <span class=u>②</span></a>
|
||||
|
||||
def __exit__(self, *args) -> None:
|
||||
'''Context management protocol. Calls close()'''
|
||||
<a> self.close() <span>③</span></a></code></pre>
|
||||
<a> self.close() <span class=u>③</span></a></code></pre>
|
||||
<ol>
|
||||
<li>The file object defines both an <code>__enter__()</code> and an <code>__exit__()</code> method. The <code>__enter__()</code> method checks that the file is open; if it’s not, the <code>_checkClosed()</code> method raises an exception.
|
||||
<li>The <code>__enter__()</code> method should almost always return <var>self</var> — this is the object that the <code>with</code> block will use to dispatch properties and methods.
|
||||
@@ -763,7 +760,7 @@ def __exit__(self, *args) -> None:
|
||||
</ol>
|
||||
|
||||
<blockquote class=note>
|
||||
<p><span>☞</span>The <code>__exit__()</code> method will always be called, even if an exception is raised inside the <code>with</code> block. In fact, if an exception is raises, the exception information will be passed to the <code>__exit__()</code> method. See <a href=http://www.python.org/doc/3.0/reference/datamodel.html#with-statement-context-managers>With Statement Context Managers</a> for more details.
|
||||
<p><span class=u>☞</span>The <code>__exit__()</code> method will always be called, even if an exception is raised inside the <code>with</code> block. In fact, if an exception is raises, the exception information will be passed to the <code>__exit__()</code> method. See <a href=http://www.python.org/doc/3.0/reference/datamodel.html#with-statement-context-managers>With Statement Context Managers</a> for more details.
|
||||
</blockquote>
|
||||
|
||||
<h2 id=esoterica>Really Esoteric Stuff</h2>
|
||||
@@ -777,11 +774,11 @@ def __exit__(self, *args) -> None:
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>a class constructor
|
||||
<td><code>x = MyClass()</code>
|
||||
<td><code class=pp>x = MyClass()</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__new__><code>x.__new__()</code></a>
|
||||
<tr><th>
|
||||
<td>a class destructor
|
||||
<td><code>del x</code>
|
||||
<td><code class=pp>del x</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__del__><code>x.__del__()</code></a>
|
||||
<tr><th>
|
||||
<td>only a specific set of attributes to be defined
|
||||
@@ -789,35 +786,36 @@ def __exit__(self, *args) -> None:
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__slots__><code>x.__slots__()</code></a>
|
||||
<tr><th>
|
||||
<td>a custom hash value
|
||||
<td><code>hash(x)</code>
|
||||
<td><code class=pp>hash(x)</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__hash__><code>x.__hash__()</code></a>
|
||||
<tr><th>
|
||||
<td>to get an attribute’s value
|
||||
<td><code>x.color</code>
|
||||
<td><code class=pp>x.color</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__get__><code>type(x).__dict__['color'].__get__(x, type(x))</code></a>
|
||||
<tr><th>
|
||||
<td>to set an attribute’s value
|
||||
<td><code>x.color = 'PapayaWhip'</code>
|
||||
<td><code class=pp>x.color = 'PapayaWhip'</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__set__><code>type(x).__dict__['color'].__set__(x, 'PapayaWhip')</code></a>
|
||||
<tr><th>
|
||||
<td>to delete an attribute
|
||||
<td><code>del x.color</code>
|
||||
<td><code class=pp>del x.color</code>
|
||||
<td><a href=http://www.python.org/doc/3.0/reference/datamodel.html#object.__delete__><code>type(x).__dict__['color'].__del__(x)</code></a>
|
||||
<tr><th>
|
||||
<td>to control whether an object is an instance of your class
|
||||
<td><code>isinstance(x, MyClass)</code>
|
||||
<td><code class=pp>isinstance(x, MyClass)</code>
|
||||
<td><a href=http://www.python.org/dev/peps/pep-3119/#overloading-isinstance-and-issubclass><code>MyClass.__instancecheck__(x)</code></a>
|
||||
<tr><th>
|
||||
<td>to control whether a class is a subclass of your class
|
||||
<td><code>issubclass(C, MyClass)</code>
|
||||
<td><code class=pp>issubclass(C, MyClass)</code>
|
||||
<td><a href=http://www.python.org/dev/peps/pep-3119/#overloading-isinstance-and-issubclass><code>MyClass.__subclasscheck__(C)</code></a>
|
||||
<tr><th>
|
||||
<td>to control whether a class is a subclass of your abstract base class
|
||||
<td><code>issubclass(C, MyABC)</code>
|
||||
<td><code class=pp>issubclass(C, MyABC)</code>
|
||||
<td><a href=http://docs.python.org/3.0/library/abc.html#abc.ABCMeta.__subclasshook__><code>MyABC.__subclasshook__(C)</code></a>
|
||||
</table>
|
||||
|
||||
<p class=v><a href=porting-code-to-python-3-with-2to3.html rel=prev title='back to “Porting code to Python 3 with 2to3”'><span>☜</span></a> <a rel=next class=todo><span>☞</span></a>
|
||||
<p class=v><a href=porting-code-to-python-3-with-2to3.html rel=prev title='back to “Porting code to Python 3 with 2to3”'><span class=u>☜</span></a> <a rel=next class=todo><span class=u>☞</span></a>
|
||||
<p class=c>© 2001–9 <a href=about.html>Mark Pilgrim</a>
|
||||
<script src=j/jquery.js></script>
|
||||
<script src=j/prettify.js></script>
|
||||
<script src=j/dip3.js></script>
|
||||
|
||||
Reference in New Issue
Block a user