mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
some work on special method names
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
<!DOCTYPE html>
|
||||
<head>
|
||||
<meta charset=utf-8>
|
||||
<title>Advanced Classes - Dive into Python 3</title>
|
||||
<!--[if IE]><script src=html5.js></script><![endif]-->
|
||||
<link rel=stylesheet type=text/css href=dip3.css>
|
||||
<style>
|
||||
body{counter-reset:h1 11}
|
||||
</style>
|
||||
<link rel=stylesheet type=text/css media='only screen and (max-device-width: 480px)' href=mobile.css>
|
||||
</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#advanced-classes>Dive Into Python 3</a> <span>‣</span>
|
||||
<p id=level>Difficulty level: <span title=advanced>♦♦♦♦♢</span>
|
||||
<h1>Advanced Classes</h1>
|
||||
<blockquote class=q>
|
||||
<p><span>❝</span> FIXME <span>❞</span><br>— FIXME
|
||||
</blockquote>
|
||||
<p id=toc>
|
||||
<h2 id=divingin>Diving In</h2>
|
||||
<p class=f>FIXME
|
||||
|
||||
<p class=d>[<a href=examples/ordereddict.py>download <code>ordereddict.py</code></a>]
|
||||
<pre><code>import collections
|
||||
import itertools
|
||||
|
||||
class OrderedDict(dict, collections.MutableMapping):
|
||||
|
||||
def __init__(self, *args, **kwds):
|
||||
if len(args) > 1:
|
||||
raise TypeError('expected at most 1 arguments')
|
||||
if not hasattr(self, '_keys'):
|
||||
self._keys = []
|
||||
self.update(*args, **kwds)
|
||||
|
||||
def clear(self):
|
||||
del self._keys[:]
|
||||
dict.clear(self)
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
if key not in self:
|
||||
self._keys.append(key)
|
||||
dict.__setitem__(self, key, value)
|
||||
|
||||
def __delitem__(self, key):
|
||||
dict.__delitem__(self, key)
|
||||
self._keys.remove(key)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._keys)
|
||||
|
||||
def __reversed__(self):
|
||||
return reversed(self._keys)
|
||||
|
||||
def popitem(self):
|
||||
if not self:
|
||||
raise KeyError('dictionary is empty')
|
||||
key = self._keys.pop()
|
||||
value = dict.pop(self, key)
|
||||
return key, value
|
||||
|
||||
def __reduce__(self):
|
||||
items = [[k, self[k]] for k in self]
|
||||
inst_dict = vars(self).copy()
|
||||
inst_dict.pop('_keys', None)
|
||||
return (self.__class__, (items,), inst_dict)
|
||||
|
||||
setdefault = MutableMapping.setdefault
|
||||
update = MutableMapping.update
|
||||
pop = MutableMapping.pop
|
||||
keys = MutableMapping.keys
|
||||
values = MutableMapping.values
|
||||
items = MutableMapping.items
|
||||
|
||||
def __repr__(self):
|
||||
if not self:
|
||||
return '%s()' % (self.__class__.__name__,)
|
||||
return '%s(%r)' % (self.__class__.__name__, list(self.items()))
|
||||
|
||||
def copy(self):
|
||||
return self.__class__(self)
|
||||
|
||||
@classmethod
|
||||
def fromkeys(cls, iterable, value=None):
|
||||
d = cls()
|
||||
for key in iterable:
|
||||
d[key] = value
|
||||
return d
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, OrderedDict):
|
||||
return all(p==q for p, q in itertools.zip_longest(self.items(), other.items()))
|
||||
return dict.__eq__(self, other)</code></pre>
|
||||
|
||||
<p class=nav><a rel=prev class=todo><span>☜</a> <a rel=next class=todo><span>☞</span></a>
|
||||
<p class=c>© 2001–9 <a href=about.html>Mark Pilgrim</a>
|
||||
<script src=jquery.js></script>
|
||||
<script src=dip3.js></script>
|
||||
+5
-3
@@ -6,8 +6,9 @@
|
||||
<link rel=stylesheet type=text/css href=dip3.css>
|
||||
<style>
|
||||
h1:before{content:""}
|
||||
#a{list-style:none;margin:0 0 0 -1.7em}
|
||||
.app{list-style:none;margin:0 0 0 -1.7em}
|
||||
#a:before{content:"A. \00a0 \00a0"}
|
||||
#b:before{content:"B. \00a0 \00a0"}
|
||||
</style>
|
||||
<link rel=stylesheet type=text/css media='only screen and (max-device-width: 480px)' href=mobile.css>
|
||||
</head>
|
||||
@@ -33,17 +34,18 @@ h1:before{content:""}
|
||||
<li><a href=unit-testing.html>Unit Testing</a>
|
||||
<li class=todo>Test-first programming
|
||||
<li><a href=refactoring.html>Refactoring</a>
|
||||
<li><a href=advanced-classes.html>Advanced Classes</a>
|
||||
<li class=todo>Files
|
||||
<li class=todo>HTML processing
|
||||
<li class=todo>XML processing
|
||||
<li class=todo>Web services
|
||||
<li class=todo>Metaclasses
|
||||
<li class=todo>Performance tuning
|
||||
<li class=todo>Packaging Python libraries
|
||||
<li class=todo>Creating graphics with the Python Imaging Library
|
||||
<li class=todo>Where to go from here
|
||||
<li><a href=case-study-porting-chardet-to-python-3.html>Case Study: Porting <code>chardet</code> to Python 3</a>
|
||||
<li id=a><a href=porting-code-to-python-3-with-2to3.html>Porting Code to Python 3 with <code>2to3</code></a>
|
||||
<li id=a class=app><a href=porting-code-to-python-3-with-2to3.html>Porting Code to Python 3 with <code>2to3</code></a>
|
||||
<li id=b class=app><a href=special-method-names.html>Special Method Names</a>
|
||||
</ol>
|
||||
|
||||
<p>There is a <a href=http://hg.diveintopython3.org/>changelog</a>, a <a type=application/atom+xml href=http://hg.diveintopython3.org/atom-log>feed</a>, and <a href="http://www.reddit.com/search?q=%22Dive+Into+Python+3%22&sort=new">discussion on Reddit</a>. During development, you can download the book by cloning the Mercurial repository:
|
||||
|
||||
@@ -31,11 +31,9 @@ td pre{padding:0;border:0}
|
||||
<h2 id=print><code>print</code> statement</h2>
|
||||
<p>In Python 2, <code>print</code> was a statement. Whatever you wanted to print simply followed the <code>print</code> keyword. In Python 3, <code>print()</code> is a function — whatever you want to print is passed to <code>print()</code> like any other function.
|
||||
<table>
|
||||
<tr>
|
||||
<th class=notes>Notes
|
||||
<th class=python2>Python 2
|
||||
<th class=python3>Python 3
|
||||
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
<tr><th>①
|
||||
<td><code>print</code>
|
||||
<td><code>print()</code>
|
||||
@@ -65,7 +63,6 @@ td pre{padding:0;border:0}
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>u"PapayaWhip"</code>
|
||||
<td><code>"PapayaWhip"</code>
|
||||
@@ -83,7 +80,6 @@ td pre{padding:0;border:0}
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>unicode(anything)</code>
|
||||
<td><code>str(anything)</code>
|
||||
@@ -95,7 +91,6 @@ td pre{padding:0;border:0}
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>x = 1000000000000L</code>
|
||||
<td><code>x = 1000000000000</code>
|
||||
@@ -125,7 +120,6 @@ td pre{padding:0;border:0}
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>if x <> y:</code>
|
||||
<td><code>if x != y:</code>
|
||||
@@ -143,7 +137,6 @@ td pre{padding:0;border:0}
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>a_dictionary.has_key("PapayaWhip")</code>
|
||||
<td><code>"PapayaWhip" in a_dictionary</code>
|
||||
@@ -173,7 +166,6 @@ td pre{padding:0;border:0}
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>a_dictionary.keys()</code>
|
||||
<td><code>list(a_dictionary.keys())</code>
|
||||
@@ -205,7 +197,6 @@ td pre{padding:0;border:0}
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>import httplib</code>
|
||||
<td><code>import http.client</code>
|
||||
@@ -233,7 +224,6 @@ import CGIHttpServer</code></pre>
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>import urllib</code>
|
||||
<td><code>import urllib.request, urllib.parse, urllib.error</code>
|
||||
@@ -271,7 +261,6 @@ from urllib.error import HTTPError</code></pre>
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>import dbm</code>
|
||||
<td><code>import dbm.ndbm</code>
|
||||
@@ -295,7 +284,6 @@ import whichdb</code></pre>
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>import xmlrpclib</code>
|
||||
<td><code>import xmlrpc.client</code>
|
||||
@@ -309,7 +297,6 @@ import SimpleXMLRPCServer</code></pre>
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><pre><code>try:
|
||||
import cStringIO as StringIO
|
||||
@@ -372,7 +359,6 @@ except ImportError:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>import constants</code>
|
||||
<td><code>from . import constants</code>
|
||||
@@ -390,7 +376,6 @@ except ImportError:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>anIterator.next()</code>
|
||||
<td><code>next(anIterator)</code>
|
||||
@@ -430,7 +415,6 @@ for an_iterator in a_sequence_of_iterators:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>filter(a_function, a_sequence)</code>
|
||||
<td><code>list(filter(a_function, a_sequence))</code>
|
||||
@@ -460,7 +444,6 @@ for an_iterator in a_sequence_of_iterators:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>map(a_function, 'PapayaWhip')</code>
|
||||
<td><code>list(map(a_function, 'PapayaWhip'))</code>
|
||||
@@ -490,7 +473,6 @@ for an_iterator in a_sequence_of_iterators:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>reduce(a, b, c)</code>
|
||||
<td><pre><code>from functtools import reduce
|
||||
@@ -505,7 +487,6 @@ reduce(a, b, c)</code></pre>
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>apply(a_function, a_list_of_args)</code>
|
||||
<td><code>a_function(*a_list_of_args)</code>
|
||||
@@ -531,7 +512,6 @@ reduce(a, b, c)</code></pre>
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>intern(aString)</code>
|
||||
<td><code>sys.intern(aString)</code>
|
||||
@@ -542,7 +522,6 @@ reduce(a, b, c)</code></pre>
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>exec codeString</code>
|
||||
<td><code>exec(codeString)</code>
|
||||
@@ -564,7 +543,6 @@ reduce(a, b, c)</code></pre>
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>execfile("a_filename")</code>
|
||||
<td><code>exec(compile(open("a_filename").read(), "a_filename", "exec"))</code>
|
||||
@@ -578,7 +556,6 @@ reduce(a, b, c)</code></pre>
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>`x`</code>
|
||||
<td><code>repr(x)</code>
|
||||
@@ -596,7 +573,6 @@ reduce(a, b, c)</code></pre>
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><pre><code>try:
|
||||
import mymodule
|
||||
@@ -643,7 +619,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>raise MyException</code>
|
||||
<td><i>unchanged</i>
|
||||
@@ -669,7 +644,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>a_generator.throw(MyException)</code>
|
||||
<td><i>no change</i>
|
||||
@@ -691,7 +665,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>xrange(10)</code>
|
||||
<td><code>range(10)</code>
|
||||
@@ -721,7 +694,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>raw_input()</code>
|
||||
<td><code>input()</code>
|
||||
@@ -743,7 +715,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>a_function.func_name</code>
|
||||
<td><code>a_function.__name__</code>
|
||||
@@ -781,7 +752,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>for line in a_file.xreadlines():</code>
|
||||
<td><code>for line in a_file:</code>
|
||||
@@ -800,7 +770,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>lambda (x,): x + f(x)</code>
|
||||
<td><code>lambda x1: x1[0] + f(x1[0])</code>
|
||||
@@ -826,7 +795,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>aClassInstance.aClassMethod.im_func</code>
|
||||
<td><code>aClassInstance.aClassMethod.__func__</code>
|
||||
@@ -843,7 +811,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><pre><code>class A:
|
||||
def __nonzero__(self):
|
||||
@@ -867,7 +834,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>x = 0755</code>
|
||||
<td><code>x = 0o755</code>
|
||||
@@ -878,7 +844,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>from sys import maxint</code>
|
||||
<td><code>from sys import maxsize</code>
|
||||
@@ -896,7 +861,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>callable(anything)</code>
|
||||
<td><code>hasattr(anything, "__call__")</code>
|
||||
@@ -907,7 +871,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>zip(a, b, c)</code>
|
||||
<td><code>list(zip(a, b, c))</code>
|
||||
@@ -925,7 +888,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>x = StandardError()</code>
|
||||
<td><code>x = Exception()</code>
|
||||
@@ -939,7 +901,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>types.StringType</code>
|
||||
<td><code>bytes</code>
|
||||
@@ -965,7 +926,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>isinstance(x, (int, float, int))</code>
|
||||
<td><code>isinstance(x, (int, float))</code>
|
||||
@@ -979,7 +939,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>isinstance(x, basestring)</code>
|
||||
<td><code>isinstance(x, str)</code>
|
||||
@@ -990,7 +949,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><code>itertools.izip(a, b)</code>
|
||||
<td><code>zip(a, b)</code>
|
||||
@@ -1016,7 +974,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>sys.exc_type</code>
|
||||
<td><code>sys.exc_info()[0]</code>
|
||||
@@ -1033,7 +990,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>[i for i in 1, 2]</code>
|
||||
<td><code>[i for i in (1, 2)]</code>
|
||||
@@ -1044,7 +1000,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>
|
||||
<td><code>os.getcwdu()</code>
|
||||
<td><code>os.getcwd()</code>
|
||||
@@ -1055,7 +1010,6 @@ except:
|
||||
<tr><th>Notes
|
||||
<th>Python 2
|
||||
<th>Python 3
|
||||
|
||||
<tr><th>①
|
||||
<td><pre><code>class C(metaclass=PapayaMeta):
|
||||
pass</code></pre>
|
||||
@@ -1158,7 +1112,7 @@ do_stuff(a_list)</code></pre>
|
||||
do_stuff(a_list)</code></pre>
|
||||
</table>
|
||||
<p>FIXME: once the rest of the book is written, this appendix should contain copious links back to any chapter or section that touches on these features.
|
||||
<p class=nav><a rel=prev href=case-study-porting-chardet-to-python-3.html title="back to “Case Study: Porting chardet to Python 3”"><span>☜</span></a> <a rel=next class=todo><span>☞</span></a>
|
||||
<p class=nav><a rel=prev href=case-study-porting-chardet-to-python-3.html title="back to “Case Study: Porting chardet to Python 3”"><span>☜</span></a> <a rel=next href=special-method-names.html title="onward to “Special Method Names”"><span>☞</span></a>
|
||||
<p class=c>© 2001–9 <a href=about.html>Mark Pilgrim</a>
|
||||
<script src=jquery.js></script>
|
||||
<script src=dip3.js></script>
|
||||
|
||||
+327
-96
@@ -5,105 +5,44 @@
|
||||
<!--[if IE]><script src=html5.js></script><![endif]-->
|
||||
<link rel=stylesheet type=text/css href=dip3.css>
|
||||
<style>
|
||||
body{counter-reset:h1 11}
|
||||
h1:before{counter-increment:h1;content:"Appendix B. "}
|
||||
h2:before{counter-increment:h2;content:"B." counter(h2) ". "}
|
||||
h3:before{counter-increment:h3;content:"B." counter(h2) "." counter(h3) ". "}
|
||||
tr + tr th:first-child{font:medium 'Arial Unicode MS',FreeSerif,OpenSymbol,'DejaVu Sans',sans-serif}
|
||||
table{width:100%;border-collapse:collapse}
|
||||
th,td{width:30%;padding:0 0.5em;border:1px solid #bbb}
|
||||
th{text-align:left;vertical-align:baseline}
|
||||
td{vertical-align:top}
|
||||
th:first-child{width:10%;text-align:center}
|
||||
th,td,td pre{margin:0}
|
||||
td pre{padding:0;border:0}
|
||||
</style>
|
||||
<link rel=stylesheet type=text/css media='only screen and (max-device-width: 480px)' href=mobile.css>
|
||||
</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 id=level>Difficulty level: <span title=advanced>♦♦♦♦♢</span>
|
||||
<p id=level>Difficulty level: <span title=advanced>♦♦♦♦♦</span>
|
||||
<h1>Special Method Names</h1>
|
||||
<blockquote class=q>
|
||||
<p><span>❝</span> FIXME <span>❞</span><br>— FIXME
|
||||
</blockquote>
|
||||
<p id=toc>
|
||||
<h2 id=divingin>Diving In</h2>
|
||||
<h2 id=divingin>Diving in</h2>
|
||||
<p class=f>FIXME
|
||||
|
||||
<p class=d>[<a href=examples/ordereddict.py>download <code>ordereddict.py</code></a>]
|
||||
<pre><code>import collections
|
||||
import itertools
|
||||
|
||||
class OrderedDict(dict, collections.MutableMapping):
|
||||
|
||||
def __init__(self, *args, **kwds):
|
||||
if len(args) > 1:
|
||||
raise TypeError('expected at most 1 arguments')
|
||||
if not hasattr(self, '_keys'):
|
||||
self._keys = []
|
||||
self.update(*args, **kwds)
|
||||
|
||||
def clear(self):
|
||||
del self._keys[:]
|
||||
dict.clear(self)
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
if key not in self:
|
||||
self._keys.append(key)
|
||||
dict.__setitem__(self, key, value)
|
||||
|
||||
def __delitem__(self, key):
|
||||
dict.__delitem__(self, key)
|
||||
self._keys.remove(key)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._keys)
|
||||
|
||||
def __reversed__(self):
|
||||
return reversed(self._keys)
|
||||
|
||||
def popitem(self):
|
||||
if not self:
|
||||
raise KeyError('dictionary is empty')
|
||||
key = self._keys.pop()
|
||||
value = dict.pop(self, key)
|
||||
return key, value
|
||||
|
||||
def __reduce__(self):
|
||||
items = [[k, self[k]] for k in self]
|
||||
inst_dict = vars(self).copy()
|
||||
inst_dict.pop('_keys', None)
|
||||
return (self.__class__, (items,), inst_dict)
|
||||
|
||||
setdefault = MutableMapping.setdefault
|
||||
update = MutableMapping.update
|
||||
pop = MutableMapping.pop
|
||||
keys = MutableMapping.keys
|
||||
values = MutableMapping.values
|
||||
items = MutableMapping.items
|
||||
|
||||
def __repr__(self):
|
||||
if not self:
|
||||
return '%s()' % (self.__class__.__name__,)
|
||||
return '%s(%r)' % (self.__class__.__name__, list(self.items()))
|
||||
|
||||
def copy(self):
|
||||
return self.__class__(self)
|
||||
|
||||
@classmethod
|
||||
def fromkeys(cls, iterable, value=None):
|
||||
d = cls()
|
||||
for key in iterable:
|
||||
d[key] = value
|
||||
return d
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, OrderedDict):
|
||||
return all(p==q for p, q in itertools.zip_longest(self.items(), other.items()))
|
||||
return dict.__eq__(self, other)</code></pre>
|
||||
|
||||
<!--
|
||||
|
||||
Basics:
|
||||
<h2 id=basics>Basics</h2>
|
||||
|
||||
<pre>
|
||||
__init__ - covered in iterators.html
|
||||
__repr__ - covered in ordereddict.py
|
||||
__str__ - covered in fractions.py
|
||||
__bytes__ (*)
|
||||
__format__
|
||||
</pre>
|
||||
|
||||
Rich comparisons:
|
||||
<h2 id=rich-comparisons>Rich Comparisons</h2>
|
||||
|
||||
<pre>
|
||||
__lt__ - covered in fractions.py
|
||||
__le__ - covered in fractions.py
|
||||
__eq__ - covered in ordereddict.py, fractions.py
|
||||
@@ -112,41 +51,135 @@ __gt__ - covered in fractions.py
|
||||
__ge__ - covered in fractions.py
|
||||
__bool__ - covered in fractions.py
|
||||
__cmp__ (*)
|
||||
</pre>
|
||||
|
||||
Custom attributes:
|
||||
<h2 id=custom-attributes>Custom Attributes</h2>
|
||||
|
||||
<pre>
|
||||
__getattr__
|
||||
__getattribute__
|
||||
__setattr__
|
||||
__delattr__
|
||||
__dir__
|
||||
</pre>
|
||||
|
||||
Acts like a function:
|
||||
<h2 id=acts-like-function>Classes That Act Like Functions</h2>
|
||||
|
||||
<pre>
|
||||
__call__
|
||||
</pre>
|
||||
|
||||
Acts like a list:
|
||||
<h2 id=acts-like-list>Classes That Act Like Sequences</h2>
|
||||
|
||||
<p>FIXME sequence intro
|
||||
|
||||
<table>
|
||||
<tr><th>Notes
|
||||
<th>You Want…
|
||||
<th>So You Write…
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>length of a sequence
|
||||
<td><code>len(seq)</code>
|
||||
<td><code>seq.__len__()</code>
|
||||
<tr><th>
|
||||
<td>whether a sequence contains a specific value
|
||||
<td><code>x in seq</code>
|
||||
<td><code>seq.__contains__(<var>x</var>)</code>
|
||||
</table>
|
||||
|
||||
<!--
|
||||
__len__
|
||||
__reversed__ - covered in ordereddict.py
|
||||
__contains__
|
||||
__index__
|
||||
__getslice__ (*)
|
||||
-->
|
||||
|
||||
Acts like a dictionary:
|
||||
<h2 id=acts-like-dict>Classes That Act Like Dictionaries</h2>
|
||||
|
||||
<pre>
|
||||
__getitem__
|
||||
__setitem__ - covered in ordereddict.py
|
||||
__delitem__ - covered in ordereddict.py
|
||||
__missing__ (*)
|
||||
</pre>
|
||||
|
||||
Acts like an iterator:
|
||||
<h2 id=acts-like-iterator>Classes That Act Like Iterators</h2>
|
||||
|
||||
<!--
|
||||
<tr><th>
|
||||
<td>reversed sequence
|
||||
<td><code>reversed(seq)</code>
|
||||
<td><code>x.__reversed__()</code>
|
||||
-->
|
||||
<pre>
|
||||
__iter__ (*) - covered in iterators.html
|
||||
__next__ (*) - covered in iterators.html
|
||||
__reversed__ - covered in ordereddict.py
|
||||
</pre>
|
||||
|
||||
Acts like a number:
|
||||
<h2 id=acts-like-number>Classes That Act Like Numbers</h2>
|
||||
|
||||
<p>FIXME binary operator intro
|
||||
|
||||
<table>
|
||||
<tr><th>Notes
|
||||
<th>You Want…
|
||||
<th>So You Write…
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>addition
|
||||
<td><code>x + y</code>
|
||||
<td><code>x.__add__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>subtraction
|
||||
<td><code>x - y</code>
|
||||
<td><code>x.__sub__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>multiplication
|
||||
<td><code>x * y</code>
|
||||
<td><code>x.__mul__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>division
|
||||
<td><code>x / y</code>
|
||||
<td><code>x.__truediv__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>floor division
|
||||
<td><code>x // y</code>
|
||||
<td><code>x.__floordiv__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>modulo (remainder)
|
||||
<td><code>x % y</code>
|
||||
<td><code>x.__mod__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>floor division <i class=baa>&</i> modulo
|
||||
<td><code>divmod(x, y)</code>
|
||||
<td><code>x.__divmod__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>raise to power
|
||||
<td><code>x ** y</code>
|
||||
<td><code>x.__pow__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>left bit-shift
|
||||
<td><code>x << y</code>
|
||||
<td><code>x.__lshift__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>right bit-shift
|
||||
<td><code>x >> y</code>
|
||||
<td><code>x.__rshift__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>bitwise <code>and</code>
|
||||
<td><code>x & y</code>
|
||||
<td><code>x.__and__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>bitwise <code>xor</code>
|
||||
<td><code>x ^ y</code>
|
||||
<td><code>x.__xor__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>bitwise <code>or</code>
|
||||
<td><code>x | y</code>
|
||||
<td><code>x.__or__(<var>y</var>)</code>
|
||||
</table>
|
||||
|
||||
<!--
|
||||
__add__ - covered in fractions.py
|
||||
__sub__
|
||||
__mul__
|
||||
@@ -160,6 +193,70 @@ __rshift__
|
||||
__and__
|
||||
__xor__
|
||||
__or__
|
||||
-->
|
||||
|
||||
<p>FIXME explain circumstances under which reflected methods will be called. <!-- If <var>x</var> doesn't implement a given special method, or if it implements it but return <code>NotImplemented</code>, the Python interpreter will try a different approach — calling a special method on <var>y</var> instead of <var>x</var>.-->
|
||||
|
||||
<table>
|
||||
<tr><th>Notes
|
||||
<th>You Want…
|
||||
<th>So You Write…
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>addition
|
||||
<td><code>x + y</code>
|
||||
<td><code>y.__radd__(<var>x</var>)</code>
|
||||
<tr><th>
|
||||
<td>subtraction
|
||||
<td><code>x - y</code>
|
||||
<td><code>y.__rsub__(<var>x</var>)</code>
|
||||
<tr><th>
|
||||
<td>multiplication
|
||||
<td><code>x * y</code>
|
||||
<td><code>y.__rmul__(<var>x</var>)</code>
|
||||
<tr><th>
|
||||
<td>division
|
||||
<td><code>x / y</code>
|
||||
<td><code>y.__rtruediv__(<var>x</var>)</code>
|
||||
<tr><th>
|
||||
<td>floor division
|
||||
<td><code>x // y</code>
|
||||
<td><code>y.__rfloordiv__(<var>x</var>)</code>
|
||||
<tr><th>
|
||||
<td>modulo (remainder)
|
||||
<td><code>x % y</code>
|
||||
<td><code>y.__rmod__(<var>x</var>)</code>
|
||||
<tr><th>
|
||||
<td>floor division <i class=baa>&</i> modulo
|
||||
<td><code>divmod(x, y)</code>
|
||||
<td><code>y.__rdivmod__(<var>x</var>)</code>
|
||||
<tr><th>
|
||||
<td>raise to power
|
||||
<td><code>x ** y</code>
|
||||
<td><code>y.__rpow__(<var>x</var>)</code>
|
||||
<tr><th>
|
||||
<td>left bit-shift
|
||||
<td><code>x << y</code>
|
||||
<td><code>y.__rlshift__(<var>x</var>)</code>
|
||||
<tr><th>
|
||||
<td>right bit-shift
|
||||
<td><code>x >> y</code>
|
||||
<td><code>y.__rrshift__(<var>x</var>)</code>
|
||||
<tr><th>
|
||||
<td>bitwise <code>and</code>
|
||||
<td><code>x & y</code>
|
||||
<td><code>y.__rand__(<var>x</var>)</code>
|
||||
<tr><th>
|
||||
<td>bitwise <code>xor</code>
|
||||
<td><code>x ^ y</code>
|
||||
<td><code>y.__rxor__(<var>x</var>)</code>
|
||||
<tr><th>
|
||||
<td>bitwise <code>or</code>
|
||||
<td><code>x | y</code>
|
||||
<td><code>y.__ror__(<var>x</var>)</code>
|
||||
</table>
|
||||
|
||||
<!--
|
||||
__radd__ - covered in fractions.py
|
||||
__rsub__
|
||||
__rmul__
|
||||
@@ -172,6 +269,66 @@ __rrshift__
|
||||
__rand__
|
||||
__rxor__
|
||||
__ror__
|
||||
-->
|
||||
|
||||
<p>FIXME explain in-place augmented assignments
|
||||
|
||||
<table>
|
||||
<tr><th>Notes
|
||||
<th>You Want…
|
||||
<th>So You Write…
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>in-place addition
|
||||
<td><code>x += y</code>
|
||||
<td><code>x.__iadd__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>in-place subtraction
|
||||
<td><code>x -= y</code>
|
||||
<td><code>x.__isub__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>in-place multiplication
|
||||
<td><code>x *= y</code>
|
||||
<td><code>x.__imul__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>in-place division
|
||||
<td><code>x /= y</code>
|
||||
<td><code>x.__itruediv__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>in-place floor division
|
||||
<td><code>x //= y</code>
|
||||
<td><code>x.__ifloordiv__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>in-place modulo
|
||||
<td><code>x %= y</code>
|
||||
<td><code>x.__imod__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>in-place raise to power
|
||||
<td><code>x **= y</code>
|
||||
<td><code>x.__ipow__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>in-place left bit-shift
|
||||
<td><code>x <<= y</code>
|
||||
<td><code>x.__ilshift__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>in-place right bit-shift
|
||||
<td><code>x >>= y</code>
|
||||
<td><code>x.__irshift__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>in-place bitwise <code>and</code>
|
||||
<td><code>x &= y</code>
|
||||
<td><code>x.__iand__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>in-place bitwise <code>xor</code>
|
||||
<td><code>x ^= y</code>
|
||||
<td><code>x.__ixor__(<var>y</var>)</code>
|
||||
<tr><th>
|
||||
<td>in-place bitwise <code>or</code>
|
||||
<td><code>x |= y</code>
|
||||
<td><code>x.__ior__(<var>y</var>)</code>
|
||||
</table>
|
||||
|
||||
<!--
|
||||
__iadd__
|
||||
__isub__
|
||||
__imul__
|
||||
@@ -184,6 +341,70 @@ __irshift__
|
||||
__iand__
|
||||
__ixor__
|
||||
__ior__
|
||||
-->
|
||||
|
||||
<p>FIXME unary operator intro
|
||||
|
||||
<table>
|
||||
<tr><th>Notes
|
||||
<th>You Want…
|
||||
<th>So You Write…
|
||||
<th>And Python Calls…
|
||||
<tr><th>
|
||||
<td>negative number
|
||||
<td><code>-x</code>
|
||||
<td><code>x.__neg__()</code>
|
||||
<tr><th>
|
||||
<td>positive number
|
||||
<td><code>+x</code>
|
||||
<td><code>x.__pos__()</code>
|
||||
<tr><th>
|
||||
<td>absolute value
|
||||
<td><code>abs(x)</code>
|
||||
<td><code>x.__abs__()</code>
|
||||
<tr><th>
|
||||
<td>inverse
|
||||
<td><code>~x</code>
|
||||
<td><code>x.__invert__()</code>
|
||||
<tr><th>
|
||||
<td>complex number
|
||||
<td><code>complex(x)</code>
|
||||
<td><code>x.__complex__()</code>
|
||||
<tr><th>
|
||||
<td>integer
|
||||
<td><code>int(x)</code>
|
||||
<td><code>x.__int__()</code>
|
||||
<tr><th>
|
||||
<td>floating point number
|
||||
<td><code>float(x)</code>
|
||||
<td><code>x.__float__()</code>
|
||||
<tr><th>
|
||||
<td>number rounded to nearest integer
|
||||
<td><code>round(x)</code>
|
||||
<td><code>x.__round__()</code>
|
||||
<tr><th>
|
||||
<td>number rounded to nearest <var>n</var> digits
|
||||
<td><code>round(x, n)</code>
|
||||
<td><code>x.__round__(n)</code>
|
||||
<tr><th>
|
||||
<td>smallest integer <code>>= x</code>
|
||||
<td><code>math.ceil(x)</code>
|
||||
<td><code>x.__ceil__()</code>
|
||||
<tr><th>
|
||||
<td>largest integer <code><= x</code>
|
||||
<td><code>math.floor(x)</code>
|
||||
<td><code>x.__floor__()</code>
|
||||
<tr><th>
|
||||
<td>truncate <code>x</code> to nearest integer toward <code>0</code>
|
||||
<td><code>math.trunc(x)</code>
|
||||
<td><code>x.__trunc__()</code>
|
||||
<tr><th>
|
||||
<td>???
|
||||
<td><code>???</code>
|
||||
<td><code>x.__index__()</code>
|
||||
</table>
|
||||
|
||||
<!--
|
||||
__neg__ - covered in fractions.py
|
||||
__pos__ - covered in fractions.py
|
||||
__abs__ - covered in fractions.py
|
||||
@@ -191,13 +412,17 @@ __invert__
|
||||
__complex__
|
||||
__int__
|
||||
__float__
|
||||
__long__ (*)
|
||||
__round__ - covered in fractions.py
|
||||
__ceil__ (*) - covered in fractions.py
|
||||
__floor__ (*) - covered in fractions.py
|
||||
__trunc__ (*) - covered in fractions.py
|
||||
__index__
|
||||
-->
|
||||
|
||||
Support for pickling, see http://docs.python.org/3.0/library/pickle.html:
|
||||
<h2 id=pickle>Support For Pickling</h2>
|
||||
|
||||
<pre>
|
||||
see http://docs.python.org/3.0/library/pickle.html:
|
||||
|
||||
__copy__ (*) - covered in fractions.py
|
||||
__deepcopy__ (*) - covered in fractions.py
|
||||
@@ -207,9 +432,18 @@ __getstate__ (*)
|
||||
__setstate__ (*)
|
||||
__reduce__ (*) - covered in ordereddict.py, fractions.py
|
||||
__reduce_ex__ (*)
|
||||
</pre>
|
||||
|
||||
Really weird stuff:
|
||||
<h2 id=context-managers>Classes That Can Be Used in a <code>with</code> Block</h2>
|
||||
|
||||
<pre>
|
||||
__enter__ see http://docs.python.org/3.0/library/stdtypes.html#typecontextmanager
|
||||
__exit__
|
||||
</pre>
|
||||
|
||||
<h2 id=esoterica>Really Esoteric Stuff</h2>
|
||||
|
||||
<pre>
|
||||
__new__ - covered in fractions.py
|
||||
__del__
|
||||
__slots__
|
||||
@@ -217,15 +451,12 @@ __hash__ - covered in fractions.py
|
||||
__get__
|
||||
__set__
|
||||
__delete__
|
||||
__enter__ see http://docs.python.org/3.0/library/stdtypes.html#typecontextmanager
|
||||
__exit__
|
||||
__subclasshook__ (*) see http://docs.python.org/3.0/library/abc.html
|
||||
__instancecheck__ (*) see http://www.ibm.com/developerworks/linux/library/l-python3-2/
|
||||
__subclasscheck__ (*)
|
||||
</pre>
|
||||
|
||||
-->
|
||||
|
||||
<p class=nav><a rel=prev class=todo><span>☜</a> <a rel=next class=todo><span>☞</span></a>
|
||||
<p class=nav><a rel=prev href=porting-code-to-python-3-with-2to3.html title="back to “Porting code to Python 3 with 2to3”"><span>☜</span></a> <a rel=next class=todo><span>☞</span></a>
|
||||
<p class=c>© 2001–9 <a href=about.html>Mark Pilgrim</a>
|
||||
<script src=jquery.js></script>
|
||||
<script src=dip3.js></script>
|
||||
|
||||
+19
-4
@@ -177,6 +177,10 @@ ul li ol{margin:0;padding:0 0 0 2.5em}
|
||||
<li>Postscript
|
||||
<li>Summary
|
||||
</ol>
|
||||
<li id=advanced-classes><a href=advanced-classes.html>Advanced Classes</a>
|
||||
<ol>
|
||||
<li><a href=advanced-classes.html#divingin>Diving in</a>
|
||||
</ol>
|
||||
<li>Files
|
||||
<ol>
|
||||
<li>File objects
|
||||
@@ -229,10 +233,6 @@ ul li ol{margin:0;padding:0 0 0 2.5em}
|
||||
<li>Putting it all together
|
||||
<li>Summary
|
||||
</ol>
|
||||
<li>Metaclasses
|
||||
<ol>
|
||||
<li>...once I figure out WTF metaclasses are...
|
||||
</ol>
|
||||
<li>Performance tuning
|
||||
<ol>
|
||||
<li>Diving in
|
||||
@@ -362,6 +362,21 @@ ul li ol{margin:0;padding:0 0 0 2.5em}
|
||||
<li><a href=porting-code-to-python-3-with-2to3.html#idioms>Common idioms</a>
|
||||
</ol>
|
||||
</ol>
|
||||
<li id=special-method-names><a href=special-method-names.html>Appendix B. Special Method Names</a>
|
||||
<ol>
|
||||
<li><a href=special-method-names.html#divingin>Diving in</a>
|
||||
<li><a href=special-method-names.html#basics>Basics</a>
|
||||
<li><a href=special-method-names.html#rich-comparisons>Rich Comparisons</a>
|
||||
<li><a href=special-method-names.html#custom-attributes>Custom Attributes</a>
|
||||
<li><a href=special-method-names.html#acts-like-function>Classes That Act Like Functions</a>
|
||||
<li><a href=special-method-names.html#acts-like-list>Classes That Act Like Sequences</a>
|
||||
<li><a href=special-method-names.html#acts-like-dict>Classes That Act Like Dictionaries</a>
|
||||
<li><a href=special-method-names.html#acts-like-iterator>Classes That Act Like Iterators</a>
|
||||
<li><a href=special-method-names.html#acts-like-number>Classes That Act Like Numbers</a>
|
||||
<li><a href=special-method-names.html#pickle>Support For Pickling</a>
|
||||
<li><a href=special-method-names.html#context-managers>Classes That Can Be Used in a <code>with</code> Block</a>
|
||||
<li><a href=special-method-names.html#esoterica>Really Esoteric Stuff</a>
|
||||
</ol>
|
||||
</ul>
|
||||
<p>Orphans (not sure where these belong yet):
|
||||
<ul>
|
||||
|
||||
Reference in New Issue
Block a user