diff --git a/advanced-classes.html b/advanced-classes.html new file mode 100644 index 0000000..b078fe8 --- /dev/null +++ b/advanced-classes.html @@ -0,0 +1,98 @@ + +
+ +You are here: Home ‣ Dive Into Python 3 ‣ +
Difficulty level: ♦♦♦♦♢ +
++❝ FIXME ❞
— FIXME +
+
FIXME + +
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)
+
+© 2001–9 Mark Pilgrim + + diff --git a/index.html b/index.html index 42dd359..0de1157 100644 --- a/index.html +++ b/index.html @@ -6,8 +6,9 @@ @@ -33,17 +34,18 @@ h1:before{content:""}
chardet to Python 3
-2to3
+2to3
+There is a changelog, a feed, and discussion on Reddit. During development, you can download the book by cloning the Mercurial repository: diff --git a/porting-code-to-python-3-with-2to3.html b/porting-code-to-python-3-with-2to3.html index 1cf9369..42fe31e 100644 --- a/porting-code-to-python-3-with-2to3.html +++ b/porting-code-to-python-3-with-2to3.html @@ -31,11 +31,9 @@ td pre{padding:0;border:0}
print statementIn Python 2, print was a statement. Whatever you wanted to print simply followed the print keyword. In Python 3, print() is a function — whatever you want to print is passed to print() like any other function.
| Notes - | Python 2 - | Python 3 - + | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Notes + | Python 2 + | Python 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ① | print
| print()
@@ -65,7 +63,6 @@ td pre{padding:0;border:0}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Notes | Python 2 | Python 3 - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ① | u"PapayaWhip"
| "PapayaWhip"
@@ -83,7 +80,6 @@ td pre{padding:0;border:0}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Notes | Python 2 | Python 3 - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
unicode(anything)
| str(anything)
@@ -95,7 +91,6 @@ td pre{padding:0;border:0}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Notes | Python 2 | Python 3 - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ① | x = 1000000000000L
| x = 1000000000000
@@ -125,7 +120,6 @@ td pre{padding:0;border:0}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Notes | Python 2 | Python 3 - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ① | if x <> y:
| if x != y:
@@ -143,7 +137,6 @@ td pre{padding:0;border:0}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Notes | Python 2 | Python 3 - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ① | a_dictionary.has_key("PapayaWhip")
| "PapayaWhip" in a_dictionary
@@ -173,7 +166,6 @@ td pre{padding:0;border:0}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Notes | Python 2 | Python 3 - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ① | a_dictionary.keys()
| list(a_dictionary.keys())
@@ -205,7 +197,6 @@ td pre{padding:0;border:0}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Notes | Python 2 | Python 3 - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ① | import httplib
| import http.client
@@ -233,7 +224,6 @@ import CGIHttpServer
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Notes | Python 2 | Python 3 - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ① | import urllib
| import urllib.request, urllib.parse, urllib.error
@@ -271,7 +261,6 @@ from urllib.error import HTTPError
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Notes | Python 2 | Python 3 - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import dbm
| import dbm.ndbm
@@ -295,7 +284,6 @@ import whichdb
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Notes | Python 2 | Python 3 - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import xmlrpclib
| import xmlrpc.client
@@ -309,7 +297,6 @@ import SimpleXMLRPCServer
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Notes | Python 2 | Python 3 - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ① | |
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. -
© 2001–9 Mark Pilgrim diff --git a/special-method-names.html b/special-method-names.html index 55dba50..31bb0fb 100644 --- a/special-method-names.html +++ b/special-method-names.html @@ -5,105 +5,44 @@
You are here: Home ‣ Dive Into Python 3 ‣ -
Difficulty level: ♦♦♦♦♢ +
Difficulty level: ♦♦♦♦♦
❝ FIXME ❞
— FIXME
-
FIXME -
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)
-
-
-Acts like a dictionary:
+__getitem__ __setitem__ - covered in ordereddict.py __delitem__ - covered in ordereddict.py __missing__ (*) +-Acts like an iterator: +
__iter__ (*) - covered in iterators.html __next__ (*) - covered in iterators.html +__reversed__ - covered in ordereddict.py +-Acts like a number: +
FIXME binary operator intro + +
| Notes + | You Want… + | So You Write… + | And Python Calls… + |
|---|---|---|---|
| + | addition + | x + y
+ | x.__add__(y)
+ |
| + | subtraction + | x - y
+ | x.__sub__(y)
+ |
| + | multiplication + | x * y
+ | x.__mul__(y)
+ |
| + | division + | x / y
+ | x.__truediv__(y)
+ |
| + | floor division + | x // y
+ | x.__floordiv__(y)
+ |
| + | modulo (remainder) + | x % y
+ | x.__mod__(y)
+ |
| + | floor division & modulo + | divmod(x, y)
+ | x.__divmod__(y)
+ |
| + | raise to power + | x ** y
+ | x.__pow__(y)
+ |
| + | left bit-shift + | x << y
+ | x.__lshift__(y)
+ |
| + | right bit-shift + | x >> y
+ | x.__rshift__(y)
+ |
| + | bitwise and
+ | x & y
+ | x.__and__(y)
+ |
| + | bitwise xor
+ | x ^ y
+ | x.__xor__(y)
+ |
| + | bitwise or
+ | x | y
+ | x.__or__(y)
+ |
FIXME explain circumstances under which reflected methods will be called. + +
| Notes + | You Want… + | So You Write… + | And Python Calls… + |
|---|---|---|---|
| + | addition + | x + y
+ | y.__radd__(x)
+ |
| + | subtraction + | x - y
+ | y.__rsub__(x)
+ |
| + | multiplication + | x * y
+ | y.__rmul__(x)
+ |
| + | division + | x / y
+ | y.__rtruediv__(x)
+ |
| + | floor division + | x // y
+ | y.__rfloordiv__(x)
+ |
| + | modulo (remainder) + | x % y
+ | y.__rmod__(x)
+ |
| + | floor division & modulo + | divmod(x, y)
+ | y.__rdivmod__(x)
+ |
| + | raise to power + | x ** y
+ | y.__rpow__(x)
+ |
| + | left bit-shift + | x << y
+ | y.__rlshift__(x)
+ |
| + | right bit-shift + | x >> y
+ | y.__rrshift__(x)
+ |
| + | bitwise and
+ | x & y
+ | y.__rand__(x)
+ |
| + | bitwise xor
+ | x ^ y
+ | y.__rxor__(x)
+ |
| + | bitwise or
+ | x | y
+ | y.__ror__(x)
+ |
FIXME explain in-place augmented assignments + +
| Notes + | You Want… + | So You Write… + | And Python Calls… + |
|---|---|---|---|
| + | in-place addition + | x += y
+ | x.__iadd__(y)
+ |
| + | in-place subtraction + | x -= y
+ | x.__isub__(y)
+ |
| + | in-place multiplication + | x *= y
+ | x.__imul__(y)
+ |
| + | in-place division + | x /= y
+ | x.__itruediv__(y)
+ |
| + | in-place floor division + | x //= y
+ | x.__ifloordiv__(y)
+ |
| + | in-place modulo + | x %= y
+ | x.__imod__(y)
+ |
| + | in-place raise to power + | x **= y
+ | x.__ipow__(y)
+ |
| + | in-place left bit-shift + | x <<= y
+ | x.__ilshift__(y)
+ |
| + | in-place right bit-shift + | x >>= y
+ | x.__irshift__(y)
+ |
| + | in-place bitwise and
+ | x &= y
+ | x.__iand__(y)
+ |
| + | in-place bitwise xor
+ | x ^= y
+ | x.__ixor__(y)
+ |
| + | in-place bitwise or
+ | x |= y
+ | x.__ior__(y)
+ |
FIXME unary operator intro + +
| Notes + | You Want… + | So You Write… + | And Python Calls… + |
|---|---|---|---|
| + | negative number + | -x
+ | x.__neg__()
+ |
| + | positive number + | +x
+ | x.__pos__()
+ |
| + | absolute value + | abs(x)
+ | x.__abs__()
+ |
| + | inverse + | ~x
+ | x.__invert__()
+ |
| + | complex number + | complex(x)
+ | x.__complex__()
+ |
| + | integer + | int(x)
+ | x.__int__()
+ |
| + | floating point number + | float(x)
+ | x.__float__()
+ |
| + | number rounded to nearest integer + | round(x)
+ | x.__round__()
+ |
| + | number rounded to nearest n digits + | round(x, n)
+ | x.__round__(n)
+ |
| + | smallest integer >= x
+ | math.ceil(x)
+ | x.__ceil__()
+ |
| + | largest integer <= x
+ | math.floor(x)
+ | x.__floor__()
+ |
| + | truncate x to nearest integer toward 0
+ | math.trunc(x)
+ | x.__trunc__()
+ |
| + | ??? + | ???
+ | x.__index__()
+ |
+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__ (*) +-Really weird stuff: +
with Block+__enter__ see http://docs.python.org/3.0/library/stdtypes.html#typecontextmanager +__exit__ ++ +
__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__ (*) +---> - -
© 2001–9 Mark Pilgrim diff --git a/table-of-contents.html b/table-of-contents.html index e2af73a..afb6b0e 100644 --- a/table-of-contents.html +++ b/table-of-contents.html @@ -177,6 +177,10 @@ ul li ol{margin:0;padding:0 0 0 2.5em}
with Block
+ Orphans (not sure where these belong yet):