You are here: Home ‣ Dive Into Python 3 ‣
Difficulty level: ♦♦♦♦♦
❝ FIXME ❞
— FIXME
FIXME
__init__ - covered in iterators.html __repr__ - covered in ordereddict.py __str__ - covered in fractions.py __bytes__ (*) __format__
__lt__ - covered in fractions.py __le__ - covered in fractions.py __eq__ - covered in ordereddict.py, fractions.py __ne__ __gt__ - covered in fractions.py __ge__ - covered in fractions.py __bool__ - covered in fractions.py (__cmp__ is gone)
__getattr__ __getattribute__ __setattr__ __delattr__ __dir__
__call__
FIXME sequence intro
| Notes | You Want… | So You Write… | And Python Calls… |
|---|---|---|---|
| length of a sequence | len(seq)
| seq.__len__()
| |
| whether a sequence contains a specific value | x in seq
| seq.__contains__(x)
|
__getitem__ __setitem__ - covered in ordereddict.py __delitem__ - covered in ordereddict.py __missing__ (*)
__iter__ (*) - covered in iterators.html __next__ (*) - covered in iterators.html __reversed__ - covered in ordereddict.py
Using 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 Fraction class implements these special methods, then you can do things like this:
>>> from fractions import Fraction >>> x = Fraction(1, 3) >>> x / 3 Fraction(1, 9)
Here is the comprehensive list of special methods you need to implement a number-like class.
| 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)
|
That’s all well and good if x is an instance of a class that implements those methods. But what if it doesn’t implement one of them? Or worse, what if it implements it, but it can’t handle certain kinds of arguments? For example:
>>> from fractions import Fraction >>> x = Fraction(1, 3) >>> 1 / x Fraction(3, 1)
This is not a case of taking a Fraction and dividing it by an integer (as in the previous example). That case was straightforward: x / 3 calls x.__truediv__(3), and the __truedive__() method of the Fraction class handles all the math. But integers don’t “know” how to do arithmetic operations with fractions. So why does this example work?
The answer lies in a second set of arithmetic special methods with reflected operands. Given an arithmetic operation that takes two operands (e.g. x / y), there are two ways to go about it:
The set of special methods above take the first approach: given x / y, they provide a way for x to say “I know how to divide myself by y.” The following set of special methods tackle the second approach: they provide a way for y to say “I know how to be the denominator and divide myself into x.”
| 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)
|
But wait! There’s more! If you’re doing “in-place” operations, like x /= 3, there are even more special methods you can define.
| 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)
|
Note: for the most part, the in-place operation methods are not required. If you don’t define an in-place method for a particular operation, Python will try the methods. For example, to execute the expression x /= y, Python will:
x.__itruediv__(y). If this method is defined and returns a value other than NotImplemented, we’re done.
x.__truediv__(y). If this method is defined and returns a value other than NotImplemented, the old value of x is discarded and replaced with the return value, just as if you had done x = x / y instead.
y.__rtruediv__(y). If this method is defined and returns a value other than NotImplemented, the old value of x is discarded and replaced with the return value.
So you only need to define in-place methods like the __itruediv__() method if you want to do some special optimization for in-place operands. Otherwise Python will essentially reformulate the in-place operand to use a regular operand + a variable assignment.
There are also a few “unary” mathematical operations you can perform on number-like objects by themselves.
| 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__()
| |
| ??? FIXME what the hell is this? | ???
| x.__index__()
|
see http://docs.python.org/3.0/library/pickle.html: __copy__ (*) - covered in fractions.py __deepcopy__ (*) - covered in fractions.py __getnewargs__ (*) __getinitargs__ (*) __getstate__ (*) __setstate__ (*) __reduce__ (*) - covered in ordereddict.py, fractions.py __reduce_ex__ (*)
with Block
__enter__ see http://docs.python.org/3.0/library/stdtypes.html#typecontextmanager
__exit__
relevant excerpt from io.py:
def __enter__(self) -> "IOBase": # That's a forward reference
"""Context management protocol. Returns self."""
self._checkClosed()
return self
def __exit__(self, *args) -> None:
"""Context management protocol. Calls close()"""
self.close()
relevant excerpt from http://www.python.org/doc/3.0/reference/datamodel.html#with-statement-context-managers
object.__enter__(self)
Enter the runtime context related to this object. The with statement will bind this method’s return value to the target(s) specified in the as clause of the statement, if any.
object.__exit__(self, exc_type, exc_value, traceback)
Exit the runtime context related to this object. The parameters describe the exception that caused the context to be exited. If the context was exited without an exception, all three arguments will be None.
If an exception is supplied, and the method wishes to suppress the exception (i.e., prevent it from being propagated), it should return a true value. Otherwise, the exception will be processed normally upon exit from this method.
Note that __exit__() methods should not reraise the passed-in exception; this is the caller’s responsibility.
__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__ (*)
© 2001–9 Mark Pilgrim