mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
stub for special-method-names chapter
This commit is contained in:
@@ -10,6 +10,7 @@ body{counter-reset:h1 7}
|
|||||||
</head>
|
</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>
|
<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-iterators>Dive Into Python 3</a> <span>‣</span>
|
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#advanced-iterators>Dive Into Python 3</a> <span>‣</span>
|
||||||
|
<p id=level title=advanced>Difficulty level: <span>♦</span><span>♦</span><span>♦</span><span>♦</span><span>♢</span>
|
||||||
<h1>Advanced Iterators</h1>
|
<h1>Advanced Iterators</h1>
|
||||||
<blockquote class=q>
|
<blockquote class=q>
|
||||||
<p><span>❝</span> Great fleas have little fleas upon their backs to bite ’em,<br>And little fleas have lesser fleas, and so ad infinitum. <span>❞</span><br>— Augustus De Morgan
|
<p><span>❝</span> Great fleas have little fleas upon their backs to bite ’em,<br>And little fleas have lesser fleas, and so ad infinitum. <span>❞</span><br>— Augustus De Morgan
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ del{background:#f87}
|
|||||||
</head>
|
</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>
|
<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#case-study-porting-chardet-to-python-3>Dive Into Python 3</a> <span>‣</span>
|
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#case-study-porting-chardet-to-python-3>Dive Into Python 3</a> <span>‣</span>
|
||||||
|
<p id=level title=pro>Difficulty level: <span>♦</span><span>♦</span><span>♦</span><span>♦</span><span>♦</span>
|
||||||
<h1>Case Study: Porting <code>chardet</code> to Python 3</h1>
|
<h1>Case Study: Porting <code>chardet</code> to Python 3</h1>
|
||||||
<blockquote class=q>
|
<blockquote class=q>
|
||||||
<p><span>❝</span> Words, words. They’re all we have to go on. <span>❞</span><br>— <a href=http://www.imdb.com/title/tt0100519/quotes>Rosencrantz and Guildenstern are Dead</a>
|
<p><span>❝</span> Words, words. They’re all we have to go on. <span>❞</span><br>— <a href=http://www.imdb.com/title/tt0100519/quotes>Rosencrantz and Guildenstern are Dead</a>
|
||||||
@@ -1179,7 +1180,6 @@ tests\EUC-JP\arclamp.jp.xml EUC-JP with confide
|
|||||||
</ol>
|
</ol>
|
||||||
|
|
||||||
<p class=nav><a rel=prev class=todo><span>☜</a> <a rel=next href=porting-code-to-python-3-with-2to3.html title="onward to “Porting Code to Python 3 with 2to3”"><span>☞</span></a>
|
<p class=nav><a rel=prev class=todo><span>☜</a> <a rel=next href=porting-code-to-python-3-with-2to3.html title="onward to “Porting Code to Python 3 with 2to3”"><span>☞</span></a>
|
||||||
|
|
||||||
<p class=c>© 2001–9 <a href=about.html>Mark Pilgrim</a>
|
<p class=c>© 2001–9 <a href=about.html>Mark Pilgrim</a>
|
||||||
<script src=jquery.js></script>
|
<script src=jquery.js></script>
|
||||||
<script src=dip3.js></script>
|
<script src=dip3.js></script>
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ p,ul,ol{margin:1.75em 0;font-size:medium}
|
|||||||
html{background:#fff;color:#222}
|
html{background:#fff;color:#222}
|
||||||
body{margin:1.75em 28px}
|
body{margin:1.75em 28px}
|
||||||
.c{clear:both;text-align:center;margin:2.154em 0}
|
.c{clear:both;text-align:center;margin:2.154em 0}
|
||||||
form div{float:right}
|
form div,#level{float:right}
|
||||||
.todo{color:#ddd}
|
.todo{color:#ddd}
|
||||||
|
|
||||||
/* links */
|
/* links */
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
#
|
||||||
|
# OrderedDict will be part of the collections module in Python 3.1.
|
||||||
|
# This is a standalone version for demonstration purposes only.
|
||||||
|
# If you're using Python 3.0, you should upgrade to Python 3.1, then
|
||||||
|
# >>> from collections import OrderedDict
|
||||||
|
# instead of using this version.
|
||||||
|
#
|
||||||
|
# Provenance:
|
||||||
|
# - PEP 372: OrderedDict http://www.python.org/dev/peps/pep-0372/
|
||||||
|
# - Python bug 5397: implement PEP 372 http://bugs.python.org/issue5397
|
||||||
|
# - Bug 5397 attachment: http://bugs.python.org/file13231/od7.diff
|
||||||
|
#
|
||||||
|
|
||||||
|
from collections import MutableMapping
|
||||||
|
from itertools import zip_longest as _zip_longest
|
||||||
|
|
||||||
|
class OrderedDict(dict, 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 _zip_longest(self.items(), other.items()))
|
||||||
|
return dict.__eq__(self, other)
|
||||||
@@ -10,6 +10,7 @@ body{counter-reset:h1 5}
|
|||||||
</head>
|
</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>
|
<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#generators>Dive Into Python 3</a> <span>‣</span>
|
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#generators>Dive Into Python 3</a> <span>‣</span>
|
||||||
|
<p id=level title=intermediate>Difficulty level: <span>♦</span><span>♦</span><span>♦</span><span>♢</span><span>♢</span>
|
||||||
<h1>Generators</h1>
|
<h1>Generators</h1>
|
||||||
<blockquote class=q>
|
<blockquote class=q>
|
||||||
<p><span>❝</span> My spelling is Wobbly. It's good spelling but it Wobbles, and the letters get in the wrong places. <span>❞</span><br>— Winnie-the-Pooh
|
<p><span>❝</span> My spelling is Wobbly. It's good spelling but it Wobbles, and the letters get in the wrong places. <span>❞</span><br>— Winnie-the-Pooh
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<head>
|
||||||
|
<meta name=robots content=noindex>
|
||||||
|
<meta charset=utf-8>
|
||||||
|
<title>Secret Leftover Page - Dive into Python 3</title>
|
||||||
|
<link rel=stylesheet type=text/css href=dip3.css>
|
||||||
|
<style>
|
||||||
|
body{counter-reset:h1 -1}
|
||||||
|
h1:before{counter-increment:h1;content:""}
|
||||||
|
</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>Dive Into Python 3</a> <span>‣</span>
|
||||||
|
<h1>Secret Leftover Page</h1>
|
||||||
|
<blockquote class=q>
|
||||||
|
<p><span>❝</span> You step in the stream / but the water has moved on. / This page is not here. <span>❞</span><br>— 404 Not Found haiku
|
||||||
|
</blockquote>
|
||||||
|
<p id=toc>
|
||||||
|
<h2 id=divingin>Huh?</h2>
|
||||||
|
<p class=f>This book used to have a chapter called “Iterators <i class=baa>&</i> Generators,” but I split the chapter in half so I could introduce Python classes before talking about iterators. The content that used to be at this address is now in one of those two chapters:
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><a href=generators.html>Generators</a>
|
||||||
|
<li><a href=iterators.html>Iterators</a>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
<p>But since you’re here, I’d like to talk about some of the small stuff I sweated while writing this book.
|
||||||
|
|
||||||
|
<h2 id=typography>Typography</h2>
|
||||||
|
|
||||||
|
<p>vertical rhythm, best available ampersand, curly quotes/apostrophes, other stuff from webtypography.net
|
||||||
|
|
||||||
|
<h2 id=graphics>Graphics</h2>
|
||||||
|
|
||||||
|
<p>Unicode, callouts, font-family issues on Windows
|
||||||
|
|
||||||
|
<h2 id=performance>Performance</h2>
|
||||||
|
|
||||||
|
<p>"Dive Into History 2009 edition", minimizing CSS + JS + HTML, inline CSS, async jQuery
|
||||||
|
|
||||||
|
<h2 id=fun>Fun stuff</h2>
|
||||||
|
|
||||||
|
<p>Quotes, constrained writing, PapayaWhip
|
||||||
|
|
||||||
|
<h2 id=furtherreading>Further Reading</h2>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><a href="http://webtypography.net/toc/">The Elements of Typographic Style Applied to the Web</a>
|
||||||
|
<li><a href="http://www.alistapart.com/articles/settingtypeontheweb">Setting Type on the Web to a Baseline Grid</a>
|
||||||
|
<li><a href="http://24ways.org/2006/compose-to-a-vertical-rhythm">Compose to a Vertical Rhythm</a>
|
||||||
|
<li><a href="http://simplebits.com/notebook/2008/08/14/ampersands.html">Use the Best Available Ampersand</a>
|
||||||
|
<li><a href="http://alanwood.net/unicode/">Unicode Support in HTML, Fonts, and Web Browsers</a>
|
||||||
|
<li><a href="http://developer.yahoo.com/yslow/">YSlow</a> for <a href="http://getfirebug.com/">Firebug</a>
|
||||||
|
<li><a href="http://developer.yahoo.com/performance/rules.html">Best Practices for Speeding Up Your Web Site</a>
|
||||||
|
<li><a href="http://stevesouders.com/hpws/rules.php">14 Rules for Faster-Loading Web Sites</a>
|
||||||
|
<li><a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a>
|
||||||
|
<li><a href="http://code.google.com/apis/ajaxlibs/">Google AJAX Libraries API</a>
|
||||||
|
</ul>
|
||||||
|
-->
|
||||||
|
|
||||||
|
<p class=c>© 2001–9 <a href=about.html>Mark Pilgrim</a>
|
||||||
|
<script src=jquery.js></script>
|
||||||
|
<script src=dip3.js></script>
|
||||||
+6
-2
@@ -10,13 +10,16 @@ body{counter-reset:h1 6}
|
|||||||
</head>
|
</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>
|
<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#iterators>Dive Into Python 3</a> <span>‣</span>
|
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#iterators>Dive Into Python 3</a> <span>‣</span>
|
||||||
|
<p id=level title=intermediate>Difficulty level: <span>♦</span><span>♦</span><span>♦</span><span>♢</span><span>♢</span>
|
||||||
<h1>Iterators</h1>
|
<h1>Iterators</h1>
|
||||||
<blockquote class=q>
|
<blockquote class=q>
|
||||||
<p><span>❝</span> East is East, and West is West, and never the twain shall meet. <span>❞</span><br>— <a href=http://en.wikiquote.org/wiki/Rudyard_Kipling>Rudyard Kipling</a>
|
<p><span>❝</span> East is East, and West is West, and never the twain shall meet. <span>❞</span><br>— <a href=http://en.wikiquote.org/wiki/Rudyard_Kipling>Rudyard Kipling</a>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
<p id=toc>
|
<p id=toc>
|
||||||
<h2 id=divingin>Diving In</h2>
|
<h2 id=divingin>Diving In</h2>
|
||||||
<p class=f>Generators are really just a special case of <i>iterators</i>. A function that <code>yield</code>s values is a nice, compact way of building an iterator without building an iterator. Remember <a href=generators.html#a-fibonacci-generator>the Fibonacci generator</a>? Here it is as a built-from-scratch iterator:
|
<p class=f>Generators are really just a special case of <i>iterators</i>. A function that <code>yield</code>s values is a nice, compact way of building an iterator without building an iterator. Let me show you what I mean by that.
|
||||||
|
|
||||||
|
<p>Remember <a href=generators.html#a-fibonacci-generator>the Fibonacci generator</a>? Here it is as a built-from-scratch iterator:
|
||||||
|
|
||||||
<p class=d>[<a href=examples/fibonacci2.py>download <code>fibonacci2.py</code></a>]
|
<p class=d>[<a href=examples/fibonacci2.py>download <code>fibonacci2.py</code></a>]
|
||||||
<pre><code>class Fib:
|
<pre><code>class Fib:
|
||||||
@@ -48,10 +51,11 @@ body{counter-reset:h1 6}
|
|||||||
|
|
||||||
<pre><code>
|
<pre><code>
|
||||||
class PapayaWhip: <span>①</span>
|
class PapayaWhip: <span>①</span>
|
||||||
pass <span>②</span></pre>
|
pass <span>②</span></code></pre>
|
||||||
<ol>
|
<ol>
|
||||||
<li>The name of this class is <code>PapayaWhip</code>, and it doesn't inherit from any other class. Class names are usually capitalized, <code>EachWordLikeThis</code>, but this is only a convention, not a requirement.
|
<li>The name of this class is <code>PapayaWhip</code>, and it doesn't inherit from any other class. Class names are usually capitalized, <code>EachWordLikeThis</code>, but this is only a convention, not a requirement.
|
||||||
<li>You probably guessed this, but everything in a class is indented, just like the code within a function, <code>if</code> statement, <code>for</code> loop, or any other block of code. The first line not indented is outside the class.
|
<li>You probably guessed this, but everything in a class is indented, just like the code within a function, <code>if</code> statement, <code>for</code> loop, or any other block of code. The first line not indented is outside the class.
|
||||||
|
</ol>
|
||||||
|
|
||||||
<p>This <code>PapayaWhip</code> class doesn't define any methods or attributes, but syntactically, there needs to be something in the definition, thus the <code>pass</code> statement. This is a Python reserved word that just means “move along, nothing to see here”. It's a statement that does nothing, and it's a good placeholder when you're stubbing out functions or classes.
|
<p>This <code>PapayaWhip</code> class doesn't define any methods or attributes, but syntactically, there needs to be something in the definition, thus the <code>pass</code> statement. This is a Python reserved word that just means “move along, nothing to see here”. It's a statement that does nothing, and it's a good placeholder when you're stubbing out functions or classes.
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ body{counter-reset:h1 2}
|
|||||||
</head>
|
</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=root value=Search></div></form>
|
<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=root value=Search></div></form>
|
||||||
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#native-datatypes>Dive Into Python 3</a> <span>‣</span>
|
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#native-datatypes>Dive Into Python 3</a> <span>‣</span>
|
||||||
|
<p id=level title=beginner>Difficulty level: <span>♦</span><span>♦</span><span>♢</span><span>♢</span><span>♢</span>
|
||||||
<h1>Native Datatypes</h1>
|
<h1>Native Datatypes</h1>
|
||||||
<blockquote class=q>
|
<blockquote class=q>
|
||||||
<p><span>❝</span> Wonder is the foundation of all philosophy, inquiry its progress, ignorance its end. <span>❞</span><br>— Michel de Montaigne
|
<p><span>❝</span> Wonder is the foundation of all philosophy, inquiry its progress, ignorance its end. <span>❞</span><br>— Michel de Montaigne
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ td pre{padding:0;border:0}
|
|||||||
</head>
|
</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>
|
<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#porting-code-to-python-3-with-2to3>Dive Into Python 3</a> <span>‣</span>
|
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#porting-code-to-python-3-with-2to3>Dive Into Python 3</a> <span>‣</span>
|
||||||
|
<p id=level title=pro>Difficulty level: <span>♦</span><span>♦</span><span>♦</span><span>♦</span><span>♦</span>
|
||||||
<h1>Porting Code to Python 3 with <code>2to3</code></h1>
|
<h1>Porting Code to Python 3 with <code>2to3</code></h1>
|
||||||
<blockquote class=q>
|
<blockquote class=q>
|
||||||
<p><span>❝</span> Life is pleasant. Death is peaceful. It’s the transition that’s troublesome. <span>❞</span><br>— Isaac Asimov (attributed)
|
<p><span>❝</span> Life is pleasant. Death is peaceful. It’s the transition that’s troublesome. <span>❞</span><br>— Isaac Asimov (attributed)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ body{counter-reset:h1 10}
|
|||||||
</head>
|
</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=root value=Search></div></form>
|
<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=root value=Search></div></form>
|
||||||
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#refactoring>Dive Into Python 3</a> <span>‣</span>
|
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#refactoring>Dive Into Python 3</a> <span>‣</span>
|
||||||
|
<p id=level title=advanced>Difficulty level: <span>♦</span><span>♦</span><span>♦</span><span>♦</span><span>♢</span>
|
||||||
<h1>Refactoring</h1>
|
<h1>Refactoring</h1>
|
||||||
<blockquote class=q>
|
<blockquote class=q>
|
||||||
<p><span>❝</span> After one has played a vast quantity of notes and more notes, it is simplicity that emerges as the crowning reward of art. <span>❞</span><br>— <a href=http://en.wikiquote.org/wiki/Fr%C3%A9d%C3%A9ric_Chopin>Frédéric Chopin</a>
|
<p><span>❝</span> After one has played a vast quantity of notes and more notes, it is simplicity that emerges as the crowning reward of art. <span>❞</span><br>— <a href=http://en.wikiquote.org/wiki/Fr%C3%A9d%C3%A9ric_Chopin>Frédéric Chopin</a>
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ body{counter-reset:h1 4}
|
|||||||
</head>
|
</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=root value=Search></div></form>
|
<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=root value=Search></div></form>
|
||||||
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#regular-expressions>Dive Into Python 3</a> <span>‣</span>
|
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#regular-expressions>Dive Into Python 3</a> <span>‣</span>
|
||||||
|
<p id=level title=intermediate>Difficulty level: <span>♦</span><span>♦</span><span>♦</span><span>♢</span><span>♢</span>
|
||||||
<h1>Regular Expressions</h1>
|
<h1>Regular Expressions</h1>
|
||||||
<blockquote class=q>
|
<blockquote class=q>
|
||||||
<p><span>❝</span> Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems. <span>❞</span><br>— <a href=http://www.jwz.org/hacks/marginal.html>Jamie Zawinski</a>
|
<p><span>❝</span> Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems. <span>❞</span><br>— <a href=http://www.jwz.org/hacks/marginal.html>Jamie Zawinski</a>
|
||||||
|
|||||||
@@ -0,0 +1,231 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<head>
|
||||||
|
<meta charset=utf-8>
|
||||||
|
<title>Special Method Names - 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#special-method-names>Dive Into Python 3</a> <span>‣</span>
|
||||||
|
<p id=level title=advanced>Difficulty level: <span>♦</span><span>♦</span><span>♦</span><span>♦</span><span>♢</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>
|
||||||
|
<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:
|
||||||
|
|
||||||
|
__init__
|
||||||
|
__repr__
|
||||||
|
__str__
|
||||||
|
__bytes__ (*)
|
||||||
|
__format__
|
||||||
|
|
||||||
|
Rich comparisons:
|
||||||
|
|
||||||
|
__lt__
|
||||||
|
__le__
|
||||||
|
__eq__
|
||||||
|
__ne__
|
||||||
|
__gt__
|
||||||
|
__ge__
|
||||||
|
__bool__
|
||||||
|
__cmp__ (*)
|
||||||
|
|
||||||
|
Custom attributes:
|
||||||
|
|
||||||
|
__getattr__
|
||||||
|
__getattribute__
|
||||||
|
__setattr__
|
||||||
|
__delattr__
|
||||||
|
__dir__
|
||||||
|
|
||||||
|
Acts like a function:
|
||||||
|
|
||||||
|
__call__
|
||||||
|
|
||||||
|
Acts like a list:
|
||||||
|
|
||||||
|
__len__
|
||||||
|
__reversed__
|
||||||
|
__contains__
|
||||||
|
__index__
|
||||||
|
__getslice__ (*)
|
||||||
|
|
||||||
|
Acts like a dictionary:
|
||||||
|
|
||||||
|
__getitem__
|
||||||
|
__setitem__
|
||||||
|
__delitem__
|
||||||
|
__missing__ (*)
|
||||||
|
|
||||||
|
Acts like an iterator:
|
||||||
|
|
||||||
|
__iter__ (*)
|
||||||
|
__next__ (*)
|
||||||
|
|
||||||
|
Acts like a number:
|
||||||
|
|
||||||
|
__add__
|
||||||
|
__sub__
|
||||||
|
__mul__
|
||||||
|
__truediv__
|
||||||
|
__floordiv__
|
||||||
|
__mod__
|
||||||
|
__divmod__
|
||||||
|
__pow__
|
||||||
|
__lshift__
|
||||||
|
__rshift__
|
||||||
|
__and__
|
||||||
|
__xor__
|
||||||
|
__or__
|
||||||
|
__radd__
|
||||||
|
__rsub__
|
||||||
|
__rmul__
|
||||||
|
__rtruediv__
|
||||||
|
__rfloordiv__
|
||||||
|
__rmod__
|
||||||
|
__rpow__
|
||||||
|
__rlshift__
|
||||||
|
__rrshift__
|
||||||
|
__rand__
|
||||||
|
__rxor__
|
||||||
|
__ror__
|
||||||
|
__iadd__
|
||||||
|
__isub__
|
||||||
|
__imul__
|
||||||
|
__itruediv__
|
||||||
|
__ifloordiv__
|
||||||
|
__imod__
|
||||||
|
__ipow__
|
||||||
|
__ilshift__
|
||||||
|
__irshift__
|
||||||
|
__iand__
|
||||||
|
__ixor__
|
||||||
|
__ior__
|
||||||
|
__neg__
|
||||||
|
__pos__
|
||||||
|
__abs__
|
||||||
|
__invert__
|
||||||
|
__complex__
|
||||||
|
__int__
|
||||||
|
__float__
|
||||||
|
__long__ (*)
|
||||||
|
__round__
|
||||||
|
__ceil__ (*)
|
||||||
|
__floor__ (*)
|
||||||
|
__trunc__ (*)
|
||||||
|
|
||||||
|
Support for pickling, see http://docs.python.org/3.0/library/pickle.html:
|
||||||
|
|
||||||
|
__copy__ (*)
|
||||||
|
__deepcopy__ (*)
|
||||||
|
__getnewargs__ (*)
|
||||||
|
__getinitargs__ (*)
|
||||||
|
__getstate__ (*)
|
||||||
|
__setstate__ (*)
|
||||||
|
__reduce__ (*)
|
||||||
|
__reduce_ex__ (*)
|
||||||
|
|
||||||
|
Really weird stuff:
|
||||||
|
|
||||||
|
__new__
|
||||||
|
__del__
|
||||||
|
__slots__
|
||||||
|
__hash__
|
||||||
|
__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__ (*)
|
||||||
|
|
||||||
|
-->
|
||||||
|
|
||||||
|
<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>
|
||||||
@@ -11,6 +11,7 @@ body{counter-reset:h1 3}
|
|||||||
</head>
|
</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>
|
<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#strings>Dive Into Python 3</a> <span>‣</span>
|
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#strings>Dive Into Python 3</a> <span>‣</span>
|
||||||
|
<p id=level title=intermediate>Difficulty level: <span>♦</span><span>♦</span><span>♦</span><span>♢</span><span>♢</span>
|
||||||
<h1>Strings</h1>
|
<h1>Strings</h1>
|
||||||
<blockquote class=q>
|
<blockquote class=q>
|
||||||
<p><span>❝</span> I’m telling you this ’cause you’re one of my friends.<br>
|
<p><span>❝</span> I’m telling you this ’cause you’re one of my friends.<br>
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ body{counter-reset:h1 8}
|
|||||||
</head>
|
</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=root value=Search></div></form>
|
<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=root value=Search></div></form>
|
||||||
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#unit-testing>Dive Into Python 3</a> <span>‣</span>
|
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#unit-testing>Dive Into Python 3</a> <span>‣</span>
|
||||||
|
<p id=level title=intermediate>Difficulty level: <span>♦</span><span>♦</span><span>♦</span><span>♢</span><span>♢</span>
|
||||||
<h1>Unit Testing</h1>
|
<h1>Unit Testing</h1>
|
||||||
<blockquote class=q>
|
<blockquote class=q>
|
||||||
<p><span>❝</span> Certitude is not the test of certainty. We have been cocksure of many things that were not so. <span>❞</span><br>— <a href=http://en.wikiquote.org/wiki/Oliver_Wendell_Holmes,_Jr.>Oliver Wendell Holmes, Jr.</a>
|
<p><span>❝</span> Certitude is not the test of certainty. We have been cocksure of many things that were not so. <span>❞</span><br>— <a href=http://en.wikiquote.org/wiki/Oliver_Wendell_Holmes,_Jr.>Oliver Wendell Holmes, Jr.</a>
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ th{text-align:left}
|
|||||||
</head>
|
</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>
|
<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#your-first-python-program>Dive Into Python 3</a> <span>‣</span>
|
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#your-first-python-program>Dive Into Python 3</a> <span>‣</span>
|
||||||
|
<p id=level title=novice>Difficulty level: <span>♦</span><span>♢</span><span>♢</span><span>♢</span><span>♢</span>
|
||||||
<h1>Your First Python Program</h1>
|
<h1>Your First Python Program</h1>
|
||||||
<blockquote class=q>
|
<blockquote class=q>
|
||||||
<p><span>❝</span> Don’t bury your burden in saintly silence. You have a problem? Great. Rejoice, dive in, and investigate. <span>❞</span><br>— <a href=http://en.wikiquote.org/wiki/Buddhism>Ven. Henepola Gunaratana</a>
|
<p><span>❝</span> Don’t bury your burden in saintly silence. You have a problem? Great. Rejoice, dive in, and investigate. <span>❞</span><br>— <a href=http://en.wikiquote.org/wiki/Buddhism>Ven. Henepola Gunaratana</a>
|
||||||
|
|||||||
Reference in New Issue
Block a user