Commit d82575d5 authored by Fred Drake's avatar Fred Drake

Markup changes in the section on disciplines to match method descriptions

a little better, and produce better HTML.

Add some index entries.
parent 8efa47b6
...@@ -784,32 +784,32 @@ Special read-only attributes: \code{start} is the lowerbound; ...@@ -784,32 +784,32 @@ Special read-only attributes: \code{start} is the lowerbound;
\section{Special method names\label{specialnames}} \section{Special method names\label{specialnames}}
A class can implement certain operations that are invoked by special A class can implement certain operations that are invoked by special
syntax (such as arithmetic operations or subscripting and slicing) by defining syntax (such as arithmetic operations or subscripting and slicing) by
methods with special names. For instance, if a class defines a defining methods with special names. For instance, if a class defines
method named \method{__getitem__()}, and \code{x} is an instance of this a method named \method{__getitem__()}, and \code{x} is an instance of
class, then \code{x[i]} is equivalent to \code{x.__getitem__(i)}. this class, then \code{x[i]} is equivalent to
(The reverse is not true --- if \code{x} is a list object, \code{x.__getitem__(i)}. (The reverse is not true --- if \code{x} is
\code{x.__getitem__(i)} is not equivalent to \code{x[i]}.) a list object, \code{x.__getitem__(i)} is not equivalent to
Except where mentioned, attempts to execute an \code{x[i]}.) Except where mentioned, attempts to execute an
operation raise an exception when no appropriate method is defined. operation raise an exception when no appropriate method is defined.
\ttindex{__getitem__} \ttindex{__getitem__}
\subsection{Basic customization\label{customization}} \subsection{Basic customization\label{customization}}
\begin{description} \begin{methoddescni}{__init__}{self\optional{, args...}}
\item[{\tt __init__(self, [args...])}]
Called when the instance is created. The arguments are those passed Called when the instance is created. The arguments are those passed
to the class constructor expression. If a base class has an to the class constructor expression. If a base class has an
\code{__init__} method the derived class's \code{__init__} method must \code{__init__} method the derived class's \code{__init__} method must
explicitly call it to ensure proper initialization of the base class explicitly call it to ensure proper initialization of the base class
part of the instance, e.g., ``\code{BaseClass.__init__(self, [args...])}''. part of the instance, e.g., \samp{BaseClass.__init__(\var{self},
[\var{args}...])}.
\ttindex{__init__} \ttindex{__init__}
\indexii{class}{constructor} \indexii{class}{constructor}
\end{methoddescni}
\item[{\tt __del__(self)}] \begin{methoddescni}{__del__}{self}
Called when the instance is about to be destroyed. This is also Called when the instance is about to be destroyed. This is also
called a destructor\index{destructor}. If a base class called a destructor\index{destructor}. If a base class
has a \method{__del__()} method, the derived class's \method{__del__()} method has a \method{__del__()} method, the derived class's \method{__del__()} method
...@@ -841,20 +841,21 @@ latter two situations can be resolved by storing None in ...@@ -841,20 +841,21 @@ latter two situations can be resolved by storing None in
\code{sys.exc_traceback} or \code{sys.last_traceback}. \code{sys.exc_traceback} or \code{sys.last_traceback}.
\strong{Warning:} due to the precarious circumstances under which \strong{Warning:} due to the precarious circumstances under which
\code{__del__} methods are invoked, exceptions that occur during their \method{__del__()} methods are invoked, exceptions that occur during their
execution are ignored, and a warning is printed to \code{sys.stderr} execution are ignored, and a warning is printed to \code{sys.stderr}
instead. Also, when \code{__del__} is invoked is response to a module instead. Also, when \method{__del__()} is invoked is response to a module
being deleted (e.g., when execution of the program is done), other being deleted (e.g., when execution of the program is done), other
globals referenced by the \code{__del__} method may already have been globals referenced by the \method{__del__()} method may already have been
deleted. For this reason, \code{__del__} methods should do the deleted. For this reason, \method{__del__()} methods should do the
absolute minimum needed to maintain external invariants. Python 1.5 absolute minimum needed to maintain external invariants. Python 1.5
guarantees that globals whose name begins with a single underscore are guarantees that globals whose name begins with a single underscore are
deleted from their module before other globals are deleted; if no deleted from their module before other globals are deleted; if no
other references to such globals exist, this may help in assuring that other references to such globals exist, this may help in assuring that
imported modules are still available at the time when the imported modules are still available at the time when the
\code{__del__} method is called. \method{__del__()} method is called.
\end{methoddescni}
\item[{\tt __repr__(self)}] \begin{methoddescni}{__repr__}{self}
Called by the \function{repr()} built-in function and by string conversions Called by the \function{repr()} built-in function and by string conversions
(reverse quotes) to compute the ``official'' string representation of (reverse quotes) to compute the ``official'' string representation of
an object. This should normally look like a valid Python expression an object. This should normally look like a valid Python expression
...@@ -868,15 +869,16 @@ may be used instead. ...@@ -868,15 +869,16 @@ may be used instead.
\indexii{reverse}{quotes} \indexii{reverse}{quotes}
\indexii{backward}{quotes} \indexii{backward}{quotes}
\index{back-quotes} \index{back-quotes}
\end{methoddescni}
\item[{\tt __str__(self)}] \begin{methoddescni}{__str__}{self}
Called by the \function{str()} built-in function and by the \keyword{print} Called by the \function{str()}\bifuncindex{str} built-in function and
statement to compute the ``informal'' string representation of an object. by the \keyword{print}\stindex{print} statement to compute the
``informal'' string representation of an object.
\ttindex{__str__} \ttindex{__str__}
\bifuncindex{str} \end{methoddescni}
\stindex{print}
\item[{\tt __cmp__(self, other)}] \begin{methoddescni}{__cmp__}{self, other}
Called by all comparison operations. Should return a negative integer if Called by all comparison operations. Should return a negative integer if
\code{self < other}, zero if \code{self == other}, a positive integer if \code{self < other}, zero if \code{self == other}, a positive integer if
\code{self > other}. If no \method{__cmp__()} operation is defined, class \code{self > other}. If no \method{__cmp__()} operation is defined, class
...@@ -886,10 +888,11 @@ instances are compared by object identity (``address''). ...@@ -886,10 +888,11 @@ instances are compared by object identity (``address'').
\ttindex{__cmp__} \ttindex{__cmp__}
\bifuncindex{cmp} \bifuncindex{cmp}
\index{comparisons} \index{comparisons}
\end{methoddescni}
\item[{\tt __hash__(self)}] \begin{methoddescni}{__hash__}{self}
Called for the key object for dictionary operations, Called for the key object for dictionary\obindex{dictionary}
and by the built-in function operations, and by the built-in function
\function{hash()}\bifuncindex{hash}. Should return a 32-bit integer \function{hash()}\bifuncindex{hash}. Should return a 32-bit integer
usable as a hash value usable as a hash value
for dictionary operations. The only required property is that objects for dictionary operations. The only required property is that objects
...@@ -904,17 +907,18 @@ implements a \method{__cmp__()} method it should not implement ...@@ -904,17 +907,18 @@ implements a \method{__cmp__()} method it should not implement
\method{__hash__()}, since the dictionary implementation requires that \method{__hash__()}, since the dictionary implementation requires that
a key's hash value is immutable (if the object's hash value changes, it a key's hash value is immutable (if the object's hash value changes, it
will be in the wrong hash bucket). will be in the wrong hash bucket).
\obindex{dictionary}
\ttindex{__cmp__} \ttindex{__cmp__}
\ttindex{__hash__} \ttindex{__hash__}
\end{methoddescni}
\item[__nonzero__(self)]
Called to implement truth value testing; should return 0 or 1. When \begin{methoddescni}{__nonzero__}{self}
this method is not defined, \code{__len__} is called, if it is defined Called to implement truth value testing; should return \code{0} or
(see below). If a class defines neither \code{__len__} nor \code{1}. When this method is not defined, \method{__len__()} is
\code{__nonzero__}, all its instances are considered true. called, if it is defined (see below). If a class defines neither
\method{__len__()} nor \method{__nonzero__()}, all its instances are
\end{description} considered true.
\ttindex{__nonzero__}
\end{methoddescni}
\subsection{Customizing attribute access\label{attribute-access}} \subsection{Customizing attribute access\label{attribute-access}}
...@@ -926,61 +930,57 @@ For performance reasons, these methods are cached in the class object ...@@ -926,61 +930,57 @@ For performance reasons, these methods are cached in the class object
at class definition time; therefore, they cannot be changed after the at class definition time; therefore, they cannot be changed after the
class definition is executed. class definition is executed.
\begin{description} \begin{methoddescni}{__getattr__}{self, name}
\item[{\tt __getattr__(self, name)}]
Called when an attribute lookup has not found the attribute in the Called when an attribute lookup has not found the attribute in the
usual places (i.e. it is not an instance attribute nor is it found in usual places (i.e. it is not an instance attribute nor is it found in
the class tree for \code{self}). \code{name} is the attribute name. the class tree for \code{self}). \code{name} is the attribute name.
This method should return the (computed) attribute value or raise an This method should return the (computed) attribute value or raise an
\code{AttributeError} exception. \exception{AttributeError} exception.
\ttindex{__getattr__} \ttindex{__getattr__}
Note that if the attribute is found through the normal mechanism, Note that if the attribute is found through the normal mechanism,
\code{__getattr__} is not called. (This is an intentional asymmetry between \method{__getattr__()} is not called. (This is an intentional
\code{__getattr__} and \code{__setattr__}.) asymmetry between \method{__getattr__()} and \method{__setattr__()}.)
This is done both for efficiency reasons and because otherwise This is done both for efficiency reasons and because otherwise
\code{__setattr__} would have no way to access other attributes of the \method{__setattr__()} would have no way to access other attributes of
instance. the instance.
Note that at least for instance variables, you can fake Note that at least for instance variables, you can fake
total control by not inserting any values in the instance total control by not inserting any values in the instance
attribute dictionary (but instead inserting them in another object). attribute dictionary (but instead inserting them in another object).
\ttindex{__setattr__} \ttindex{__setattr__}
\end{methoddescni}
\item[{\tt __setattr__(self, name, value)}] \begin{methoddescni}{__setattr__}{self, name, value}
Called when an attribute assignment is attempted. This is called Called when an attribute assignment is attempted. This is called
instead of the normal mechanism (i.e. store the value in the instance instead of the normal mechanism (i.e.\ store the value in the instance
dictionary). \code{name} is the attribute name, \code{value} is the dictionary). \var{name} is the attribute name, \var{value} is the
value to be assigned to it. value to be assigned to it.
\ttindex{__setattr__} \ttindex{__setattr__}
If \code{__setattr__} wants to assign to an instance attribute, it If \method{__setattr__()} wants to assign to an instance attribute, it
should not simply execute ``\code{self.\var{name} = value}'' --- this would should not simply execute \samp{self.\var{name} = value} --- this
cause a recursive call to itself. Instead, it should insert the value in the would cause a recursive call to itself. Instead, it should insert the
dictionary of instance attributes, e.g., value in the dictionary of instance attributes, e.g.,
``\code{self.__dict__[name] = value}.'' \samp{self.__dict__[\var{name}] = value}.
\ttindex{__dict__} \ttindex{__dict__}
\end{methoddescni}
\item[{\tt __delattr__(self, name)}] \begin{methoddescni}{__delattr__}{self, name}
Like \code{__setattr__} but for attribute deletion instead of Like \method{__setattr__()} but for attribute deletion instead of
assignment. assignment.
\ttindex{__delattr__} \ttindex{__delattr__}
\end{methoddescni}
\end{description}
\subsection{Emulating callable objects\label{callable-types}} \subsection{Emulating callable objects\label{callable-types}}
\begin{description} \begin{methoddescni}{__call__}{self\optional{, args...}}
\item[{\tt __call__(self, [args...])}]
Called when the instance is ``called'' as a function; if this method Called when the instance is ``called'' as a function; if this method
is defined, \code{x(arg1, arg2, ...)} is a shorthand for is defined, \code{\var{x}(arg1, arg2, ...)} is a shorthand for
\code{x.__call__(arg1, arg2, ...)}. \code{\var{x}.__call__(arg1, arg2, ...)}.
\ttindex{__call__} \ttindex{__call__}
\indexii{call}{instance} \indexii{call}{instance}
\end{methoddescni}
\end{description}
\subsection{Emulating sequence and mapping types\label{sequence-types}} \subsection{Emulating sequence and mapping types\label{sequence-types}}
...@@ -994,7 +994,7 @@ sequence, and the method \method{__getslice__()} (see below) should be ...@@ -994,7 +994,7 @@ sequence, and the method \method{__getslice__()} (see below) should be
defined. It is also recommended that mappings provide methods defined. It is also recommended that mappings provide methods
\method{keys()}, \method{values()}, \method{items()}, \method{keys()}, \method{values()}, \method{items()},
\method{has_key()}, \method{get()}, \method{clear()}, \method{copy()}, \method{has_key()}, \method{get()}, \method{clear()}, \method{copy()},
and \method{update()} behaving similar to those for and \method{update()} behaving similar to those for
Python's standard dictionary objects; mutable sequences should provide Python's standard dictionary objects; mutable sequences should provide
methods \method{append()}, \method{count()}, \method{index()}, methods \method{append()}, \method{count()}, \method{index()},
\method{insert()}, \method{pop()}, \method{remove()}, \method{reverse()} \method{insert()}, \method{pop()}, \method{remove()}, \method{reverse()}
...@@ -1026,35 +1026,35 @@ multiplication (meaning repetition) by defining the methods ...@@ -1026,35 +1026,35 @@ multiplication (meaning repetition) by defining the methods
\ttindex{__rmul__} \ttindex{__rmul__}
\ttindex{__coerce__} \ttindex{__coerce__}
\begin{description} \begin{methoddescni}{__len__}{self}
Called to implement the built-in function
\item[{\tt __len__(self)}] \function{len()}\bifuncindex{len}. Should return the length of the
Called to implement the built-in function \function{len()}. Should return object, an integer \code{>=} 0. Also, an object that doesn't define a
the length of the object, an integer \code{>=} 0. Also, an object \method{__nonzero__()} method and whose \method{__len__()} method
that doesn't define a \method{__nonzero__()} method and returns zero is considered to be false in a Boolean context.
whose \method{__len__()} method returns zero is considered to be false in a
Boolean context.
\ttindex{__len__} \ttindex{__len__}
\ttindex{__nonzero__} \ttindex{__nonzero__}
\end{methoddescni}
\item[{\tt __getitem__(self, key)}] \begin{methoddescni}{__getitem__}{self, key}
Called to implement evaluation of \code{self[key]}. Called to implement evaluation of \code{\var{self}[\var{key}]}.
For a sequence types, the accepted keys should be integers. Note that the For a sequence types, the accepted keys should be integers. Note that the
special interpretation of negative indices (if the class wishes to special interpretation of negative indices (if the class wishes to
emulate a sequence type) is up to the \method{__getitem__()} method. emulate a sequence type) is up to the \method{__getitem__()} method.
\ttindex{__getitem__} \ttindex{__getitem__}
\end{methoddescni}
\item[{\tt __setitem__(self, key, value)}] \begin{methoddescni}{__setitem__}{self, key, value}
Called to implement assignment to \code{self[key]}. Same note as for Called to implement assignment to \code{\var{self}[\var{key}]}. Same
\method{__getitem__()}. note as for \method{__getitem__()}.
\ttindex{__setitem__} \ttindex{__setitem__}
\end{methoddescni}
\item[{\tt __delitem__(self, key)}] \begin{methoddescni}{__delitem__}{self, key}
Called to implement deletion of \code{self[key]}. Same note as for Called to implement deletion of \code{\var{self}[\var{key}]}. Same
\method{__getitem__()}. note as for \method{__getitem__()}.
\ttindex{__delitem__} \ttindex{__delitem__}
\end{methoddescni}
\end{description}
\subsection{Additional methods for emulation of sequence types% \subsection{Additional methods for emulation of sequence types%
...@@ -1065,28 +1065,28 @@ objects. Immutable sequences methods should only define ...@@ -1065,28 +1065,28 @@ objects. Immutable sequences methods should only define
\method{__getslice__()}; mutable sequences, should define all three \method{__getslice__()}; mutable sequences, should define all three
three methods. three methods.
\begin{description} \begin{methoddescni}{__getslice__}{self, i, j}
Called to implement evaluation of \code{\var{self}[\var{i}:\var{j}]}.
\item[{\tt __getslice__(self, i, j)}] The returned object should be of the same type as \var{self}. Note
Called to implement evaluation of \code{self[i:j]}. The returned that missing \var{i} or \var{j} in the slice expression are replaced
object should be of the same type as \code{self}. Note that missing by zero or \code{sys.maxint}, respectively, and no further
\var{i} or \var{j} in the slice expression are replaced by zero or transformations on the indices is performed. The interpretation of
\code{sys.maxint}, respectively, and no further transformations on the negative indices and indices larger than the length of the sequence is
indices is performed. The interpretation of negative indices and up to the method.
indices larger than the length of the sequence is up to the method.
\ttindex{__getslice__} \ttindex{__getslice__}
\end{methoddescni}
\item[{\tt __setslice__(self, i, j, sequence)}] \begin{methoddescni}{__setslice__}{self, i, j, sequence}
Called to implement assignment to \code{self[i:j]}. Same notes for Called to implement assignment to \code{\var{self}[\var{i}:\var{j}]}.
\var{i} and \var{j} as for \method{__getslice__()}. Same notes for \var{i} and \var{j} as for \method{__getslice__()}.
\ttindex{__setslice__} \ttindex{__setslice__}
\end{methoddescni}
\item[{\tt __delslice__(self, i, j)}] \begin{methoddescni}{__delslice__}{self, i, j}
Called to implement deletion of \code{self[i:j]}. Same notes for Called to implement deletion of \code{\var{self}[\var{i}:\var{j}]}.
\var{i} and \var{j} as for \method{__getslice__()}. Same notes for \var{i} and \var{j} as for \method{__getslice__()}.
\ttindex{__delslice__} \ttindex{__delslice__}
\end{methoddescni}
\end{description}
Notice that these methods are only invoked when a single slice with a Notice that these methods are only invoked when a single slice with a
single colon is used. For slice operations involving extended slice single colon is used. For slice operations involving extended slice
...@@ -1100,31 +1100,30 @@ Methods corresponding to operations that are not supported by the ...@@ -1100,31 +1100,30 @@ Methods corresponding to operations that are not supported by the
particular kind of number implemented (e.g., bitwise operations for particular kind of number implemented (e.g., bitwise operations for
non-integral numbers) should be left undefined. non-integral numbers) should be left undefined.
\begin{description} \begin{methoddescni}{__add__}{self, other}
\methodlineni{__sub__}{self, other}
\item[{\tt __add__(self, other)}]\itemjoin \methodlineni{__mul__}{self, other}
\item[{\tt __sub__(self, other)}]\itemjoin \methodlineni{__div__}{self, other}
\item[{\tt __mul__(self, other)}]\itemjoin \methodlineni{__mod__}{self, other}
\item[{\tt __div__(self, other)}]\itemjoin \methodlineni{__divmod__}{self, other}
\item[{\tt __mod__(self, other)}]\itemjoin \methodlineni{__pow__}{self, other\optional{, modulo}}
\item[{\tt __divmod__(self, other)}]\itemjoin \methodlineni{__lshift__}{self, other}
\item[{\tt __pow__(self, other \optional{, modulo})}]\itemjoin \methodlineni{__rshift__}{self, other}
\item[{\tt __lshift__(self, other)}]\itemjoin \methodlineni{__and__}{self, other}
\item[{\tt __rshift__(self, other)}]\itemjoin \methodlineni{__xor__}{self, other}
\item[{\tt __and__(self, other)}]\itemjoin \methodlineni{__or__}{self, other}
\item[{\tt __xor__(self, other)}]\itemjoin
\item[{\tt __or__(self, other)}]\itembreak
These functions are These functions are
called to implement the binary arithmetic operations (\code{+}, called to implement the binary arithmetic operations (\code{+},
\code{-}, \code{*}, \code{/}, \code{\%}, \function{divmod()}, \function{pow()}, \code{-}, \code{*}, \code{/}, \code{\%},
\code{**}, \function{divmod()}\bifuncindex{divmod},
\code{<<}, \code{>>}, \code{\&}, \code{\^}, \code{|}). \function{pow()}\bifuncindex{pow}, \code{**}, \code{<<}, \code{>>},
For instance, to evaluate the expression \var{x}\code{+}\var{y}, where \code{\&}, \code{\^}, \code{|}). For instance, to evaluate the
\var{x} is an instance of a class that has an \method{__add__()} expression \var{x}\code{+}\var{y}, where \var{x} is an instance of a
method, \code{\var{x}.__add__(\var{y})} is called. class that has an \method{__add__()} method,
Note that \function{__pow__()} should be defined to accept an optional \code{\var{x}.__add__(\var{y})} is called. Note that
third argument if the ternary version of the built-in \function{pow()} \method{__pow__()} should be defined to accept an optional third
function is to be supported. argument if the ternary version of the built-in
\function{pow()}\bifuncindex{pow} function is to be supported.
\ttindex{__or__} \ttindex{__or__}
\ttindex{__xor__} \ttindex{__xor__}
\ttindex{__and__} \ttindex{__and__}
...@@ -1137,31 +1136,32 @@ function is to be supported. ...@@ -1137,31 +1136,32 @@ function is to be supported.
\ttindex{__mul__} \ttindex{__mul__}
\ttindex{__sub__} \ttindex{__sub__}
\ttindex{__add__} \ttindex{__add__}
\end{methoddescni}
\item[{\tt __radd__(self, other)}]\itemjoin
\item[{\tt __rsub__(self, other)}]\itemjoin \begin{methoddescni}{__radd__}{self, other}
\item[{\tt __rmul__(self, other)}]\itemjoin \methodlineni{__rsub__}{self, other}
\item[{\tt __rdiv__(self, other)}]\itemjoin \methodlineni{__rmul__}{self, other}
\item[{\tt __rmod__(self, other)}]\itemjoin \methodlineni{__rdiv__}{self, other}
\item[{\tt __rdivmod__(self, other)}]\itemjoin \methodlineni{__rmod__}{self, other}
\item[{\tt __rpow__(self, other)}]\itemjoin \methodlineni{__rdivmod__}{self, other}
\item[{\tt __rlshift__(self, other)}]\itemjoin \methodlineni{__rpow__}{self, other}
\item[{\tt __rrshift__(self, other)}]\itemjoin \methodlineni{__rlshift__}{self, other}
\item[{\tt __rand__(self, other)}]\itemjoin \methodlineni{__rrshift__}{self, other}
\item[{\tt __rxor__(self, other)}]\itemjoin \methodlineni{__rand__}{self, other}
\item[{\tt __ror__(self, other)}]\itembreak \methodlineni{__rxor__}{self, other}
\methodlineni{__ror__}{self, other}
These functions are These functions are
called to implement the binary arithmetic operations (\code{+}, called to implement the binary arithmetic operations (\code{+},
\code{-}, \code{*}, \code{/}, \code{\%}, \function{divmod()}, \function{pow()}, \code{-}, \code{*}, \code{/}, \code{\%},
\code{**}, \function{divmod()}\bifuncindex{divmod},
\code{<<}, \code{>>}, \code{\&}, \code{\^}, \code{|}) with reversed operands. \function{pow()}\bifuncindex{pow}, \code{**}, \code{<<}, \code{>>},
These functions are only called if the left operand does not support \code{\&}, \code{\^}, \code{|}) with reversed operands. These
the corresponding operation. functions are only called if the left operand does not support the
For instance, to evaluate the expression \var{x}\code{-}\var{y}, where corresponding operation. For instance, to evaluate the expression
\var{y} is an instance of a class that has an \method{__rsub__()} \var{x}\code{-}\var{y}, where \var{y} is an instance of a class that
method, \code{\var{y}.__rsub__(\var{x})} is called. has an \method{__rsub__()} method, \code{\var{y}.__rsub__(\var{x})} is
Note that ternary \function{pow()} will not try calling called. Note that ternary \function{pow()}\bifuncindex{pow} will not
\method{__rpow__()} (the coercion rules would become too try calling \method{__rpow__()} (the coercion rules would become too
complicated). complicated).
\ttindex{__or__} \ttindex{__or__}
\ttindex{__xor__} \ttindex{__xor__}
...@@ -1175,44 +1175,52 @@ complicated). ...@@ -1175,44 +1175,52 @@ complicated).
\ttindex{__mul__} \ttindex{__mul__}
\ttindex{__sub__} \ttindex{__sub__}
\ttindex{__add__} \ttindex{__add__}
\end{methoddescni}
\item[{\tt __neg__(self)}]\itemjoin \begin{methoddescni}{__neg__}{self}
\item[{\tt __pos__(self)}]\itemjoin \methodlineni{__pos__}{self}
\item[{\tt __abs__(self)}]\itemjoin \methodlineni{__abs__}{self}
\item[{\tt __invert__(self)}]\itembreak \methodlineni{__invert__}{self}
Called to implement the unary arithmetic operations (\code{-}, \code{+}, Called to implement the unary arithmetic operations (\code{-}, \code{+},
\function{abs()} and \code{~}). \function{abs()}\bifuncindex{abs} and \code{~}).
\ttindex{__invert__} \ttindex{__invert__}
\ttindex{__abs__} \ttindex{__abs__}
\ttindex{__pos__} \ttindex{__pos__}
\ttindex{__neg__} \ttindex{__neg__}
\end{methoddescni}
\item[{\tt __int__(self)}]\itemjoin
\item[{\tt __long__(self)}]\itemjoin \begin{methoddescni}{__int__}{self}
\item[{\tt __float__(self)}]\itembreak \methodlineni{__long__}{self}
Called to implement the built-in functions \function{int()}, \function{long()} \methodlineni{__float__}{self}
and \function{float()}. Should return a value of the appropriate type. Called to implement the built-in functions
\function{int()}\bifuncindex{int}, \function{long()}\bifuncindex{long}
and \function{float()}\bifuncindex{float}. Should return a value of
the appropriate type.
\ttindex{__float__} \ttindex{__float__}
\ttindex{__long__} \ttindex{__long__}
\ttindex{__int__} \ttindex{__int__}
\end{methoddescni}
\item[{\tt __oct__(self)}]\itemjoin \begin{methoddescni}{__oct__}{self}
\item[{\tt __hex__(self)}]\itembreak \methodlineni{__hex__}{self}
Called to implement the built-in functions \function{oct()} and Called to implement the built-in functions
\function{hex()}. Should return a string value. \function{oct()}\bifuncindex{oct} and
\function{hex()}\bifuncindex{hex}. Should return a string value.
\ttindex{__hex__} \ttindex{__hex__}
\ttindex{__oct__} \ttindex{__oct__}
\end{methoddescni}
\item[{\tt __coerce__(self, other)}] \begin{methoddescni}{__coerce__}{self, other}
\ttindex{__coerce__}
Called to implement ``mixed-mode'' numeric arithmetic. Should either Called to implement ``mixed-mode'' numeric arithmetic. Should either
return a 2-tuple containing \code{self} and \code{other} converted to return a 2-tuple containing \var{self} and \var{other} converted to
a common numeric type, or \code{None} if conversion is possible. When a common numeric type, or \code{None} if conversion is possible. When
the common type would be the type of \code{other}, it is sufficient to the common type would be the type of \code{other}, it is sufficient to
return \code{None}, since the interpreter will also ask the other return \code{None}, since the interpreter will also ask the other
object to attempt a coercion (but sometimes, if the implementation of object to attempt a coercion (but sometimes, if the implementation of
the other type cannot be changed, it is useful to do the conversion to the other type cannot be changed, it is useful to do the conversion to
the other type here). the other type here).
\ttindex{__coerce__} \end{methoddescni}
\strong{Coercion rules}: to evaluate \var{x} \var{op} \var{y}, the \strong{Coercion rules}: to evaluate \var{x} \var{op} \var{y}, the
following steps are taken (where \method{__op__()} and following steps are taken (where \method{__op__()} and
...@@ -1281,5 +1289,3 @@ instance. ...@@ -1281,5 +1289,3 @@ instance.
\end{itemize} \end{itemize}
\end{itemize} \end{itemize}
\end{description}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment