diff --git a/about.html b/about.html index 9db0863..79ec50b 100644 --- a/about.html +++ b/about.html @@ -12,15 +12,15 @@ h1:before{content:""}
You are here: Home ‣ Dive Into Python 3 ‣
The text of Dive Into Python 3 is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. -
The chardet library referenced in Case study: porting chardet to Python 3 is licensed under the LGPL 2.1 or later. All other example code is licensed under the MIT license. Full licensing terms are included in each source code file.
-
The dynamic highlighting effects in the online edition are built on top of jQuery, which is dual-licensed under the MIT and GPL licenses. +
The chardet library referenced in Case study: porting chardet to Python 3 is licensed under the LGPL 2.1 or later. The alphametics solver referenced in Advanced Iterators is based on Raymond Hettinger's solver for Python 2, which he has graciously relicensed under the MIT license so I could port it to Python 3. Advanced Classes and Special Method Names contain snippets of code from the Python standard library which are released under the Python Software Foundation License version 2. All other example code is my original work and is licensed under the MIT license. Full licensing terms are included in each source code file.
+
The dynamic highlighting effects in the online edition are built on top of jQuery, which is dual-licensed under the MIT and GPL licenses.
The online edition loads as quickly as it does because
Send corrections and feedback to mark@diveintomark.org.
© 2001–9 Mark Pilgrim diff --git a/special-method-names.html b/special-method-names.html index 7f3f231..d5da949 100644 --- a/special-method-names.html +++ b/special-method-names.html @@ -29,11 +29,11 @@ td a:link, td a:visited{border:0}
FIXME +
We’ve already covered a few special method names elsewhere in this book — “magic” methods that Python invokes when you use certain syntax. Using special methods, your classes can act like sequences, like dictionaries, like functions, like iterators, or even like numbers! This appendix serves both as a reference for the special methods we’ve seen already and a brief introduction to some of the more esoteric ones.
If you’ve read the introduction to classes, you’ve already seen the most common special method: the __init__() method. The majority of classes I write end up needing some initialization.
+
If you’ve read the introduction to classes, you’ve already seen the most common special method: the __init__() method. The majority of classes I write end up needing some initialization. There are also a few other basic special methods that are especially useful for debugging your custom classes.
Notes
@@ -66,7 +66,7 @@ td a:link, td a:visited{border:0}
__repr__() method should return a string that is a valid Python expression.
__str__() method is also called when you print(x).
bytes type was introduced.
-decimal.py in the Python standard library provides its own __format__() method.
Classes That Act Like Iterators@@ -138,13 +138,13 @@ td a:link, td a:visited{border:0}The distinction between the ->>> class Dynamo: -... def __getattr__(self, key): -... if key == "color": ① -... return "PapayaWhip" -... else: -... raise AttributeError ② -... +
The
+
Classes That Act Like Sequences-FIXME sequence intro + If your class acts as a container for a set of values — that is, if it makes sense to ask whether your class “contains” a value — then it should probably define the following special methods that make it act like a sequence.
The
+
Classes That Act Like Dictionaries-FIXME + Extending the previous section a bit, you can define classes that not only respond to the “
The
+
Classes That Act Like NumbersUsing the appropriate special methods, you can define your own classes that act like numbers. That is, you can add them, subtract them, and perform other mathematical operations on them. This is how fractions are implemented — the truncate | x to nearest integer toward 0
math.trunc(x)
x.__trunc__()
-
- | ??? FIXME what the hell is this?
- | ???
+PEP 357
+ | number as a list index
+ | a_list[x]
x.__index__()
|
|---|
x.__bool__()
--see http://docs.python.org/3.0/library/pickle.html: ++Python supports serializing and unserializing arbitrary objects. (Most Python references call this process “pickling” and “unpickling.”) This can be useful for saving state to a file and restoring it later. All of the native datatypes support pickling already. If you create a custom class that you to be able to pickle, read up on the pickle protocol to see when and how the following special methods are called. -__copy__ (*) - covered in fractions.py -__deepcopy__ (*) - covered in fractions.py -__getnewargs__ (*) -__getinitargs__ (*) -__getstate__ (*) -__setstate__ (*) -__reduce__ (*) - covered in ordereddict.py, fractions.py -__reduce_ex__ (*) -
| Notes + | You Want… + | So You Write… + | And Python Calls… + |
|---|---|---|---|
| + | a custom object copy + | copy.copy(x)
+ | x.__copy__()
+ |
| + | a custom object deepcopy + | copy.deepcopy(x)
+ | x.__deepcopy__()
+ |
| + | to get an object’s state before pickling + | pickle.dump(x, file)
+ | x.__getstate__()
+ |
| + | to serialize an object + | pickle.dump(x, file)
+ | x.__reduce__()
+ |
| + | to serialize an object (new pickling protocol) + | pickle.dump(x, file, protocol_version)
+ | x.__reduce_ex__(protocol_version)
+ |
| + | control over how an object is created during unpickling + | x = pickle.load(file)
+ | x.__getnewargs__()
+ |
| + | to restore an object’s state after unpickling + | x = pickle.load(file)
+ | x.__setstate__()
+ |
with Block-__new__ - covered in fractions.py -__del__ -__slots__ -__hash__ - covered in fractions.py -__get__ -__set__ -__delete__ -__subclasshook__ (*) see http://docs.python.org/3.0/library/abc.html -__instancecheck__ (*) see http://www.ibm.com/developerworks/linux/library/l-python3-2/ -__subclasscheck__ (*) -+
If you know what you’re doing, you can gain almost complete control over how classes are compared, how attributes are defined, and what kinds of classes are considered subclasses of your class. + +
| Notes + | You Want… + | So You Write… + | And Python Calls… + |
|---|---|---|---|
| + | a class constructor + | x = MyClass()
+ | x.__new__()
+ |
| + | a class destructor + | del x
+ | x.__del__()
+ |
| + | only a specific set of attributes to be defined + | + | x.__slots__()
+ |
| + | a custom hash value + | hash(x)
+ | x.__hash__()
+ |
| + | to get an attribute’s value + | x.color
+ | type(x).__dict__['color'].__get__(x, type(x))
+ |
| + | to set an attribute’s value + | x.color = 'PapayaWhip'
+ | type(x).__dict__['color'].__set__(x, 'PapayaWhip')
+ |
| + | to delete an attribute + | del x.color
+ | type(x).__dict__['color'].__del__(x)
+ |
| + | to control whether an object is an instance of your class + | isinstance(x, MyClass)
+ | MyClass.__instancecheck__(x)
+ |
| + | to control whether a class is a subclass of your class + | issubclass(C, MyClass)
+ | MyClass.__subclasscheck__(C)
+ |
| + | to control whether a class is a subclass of your abstract base class + | issubclass(C, MyABC)
+ | MyABC.__subclasshook__(C)
+ |
© 2001–9 Mark Pilgrim diff --git a/table-of-contents.html b/table-of-contents.html index db1b74c..e9d621a 100644 --- a/table-of-contents.html +++ b/table-of-contents.html @@ -135,29 +135,28 @@ ul li ol{margin:0;padding:0 0 0 2.5em}
itertools Module
- itertools Module
+ with Block