Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
1e42d8aa
Kaydet (Commit)
1e42d8aa
authored
Kas 25, 1998
tarafından
Fred Drake
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Massive change to just about every construct that impacts the index.
Blame it on Just. ;-)
üst
ea81edf0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
210 additions
and
225 deletions
+210
-225
ref3.tex
Doc/ref/ref3.tex
+210
-225
No files found.
Doc/ref/ref3.tex
Dosyayı görüntüle @
1e42d8aa
...
...
@@ -110,8 +110,9 @@ object, if it has any.
\index
{
attribute
}
\indexii
{
special
}{
attribute
}
\indexiii
{
generic
}{
special
}{
attribute
}
\ttindex
{__
methods
__}
\ttindex
{__
members
__}
\withsubitem
{
(built-in object attribute)
}{
%
\ttindex
{__
methods
__}
\ttindex
{__
members
__}}
\begin{description}
...
...
@@ -387,12 +388,13 @@ defines the global namespace of the module in which the function was
defined. Additional information about a function's definition can be
retrieved from its code object; see the description of internal types
below.
\ttindex
{
func
_
doc
}
\ttindex
{__
doc
__}
\ttindex
{__
name
__}
\ttindex
{
func
_
defaults
}
\ttindex
{
func
_
code
}
\ttindex
{
func
_
globals
}
\withsubitem
{
(function attribute)
}{
%
\ttindex
{
func
_
doc
}
%
\ttindex
{__
doc
__}
%
\ttindex
{__
name
__}
%
\ttindex
{
func
_
defaults
}
%
\ttindex
{
func
_
code
}
%
\ttindex
{
func
_
globals
}}
\indexii
{
global
}{
namespace
}
\item
[User-defined methods]
...
...
@@ -409,6 +411,9 @@ base class of the class of which \member{im_self} is an instance);
\member
{__
doc
__}
is the method's documentation (same as
\code
{
im
_
func.
__
doc
__}
);
\member
{__
name
__}
is the method name (same as
\code
{
im
_
func.
__
name
__}
).
\withsubitem
{
(method attribute)
}{
%
\ttindex
{
im
_
func
}
%
\ttindex
{
im
_
self
}}
User-defined method objects are created in two ways: when getting an
attribute of a class that is a user-defined function object, or when
...
...
@@ -425,6 +430,10 @@ function \method{f()}, \code{C.f} does not yield the function object
instance,
\code
{
x.f
}
yields a bound method object
\code
{
m
}
where
\code
{
m.im
_
class
}
is
\code
{
C
}
,
\code
{
m.im
_
func
}
is
\method
{
f()
}
, and
\code
{
m.im
_
self
}
is
\code
{
x
}
.
\withsubitem
{
(method attribute)
}{
%
\ttindex
{
im
_
class
}
%
\ttindex
{
im
_
func
}
%
\ttindex
{
im
_
self
}}
When an unbound user-defined method object is called, the underlying
function (
\member
{
im
_
func
}
) is called, with the restriction that the
...
...
@@ -446,9 +455,6 @@ Also notice that this transformation only happens for user-defined
functions; other callable objects (and all non-callable objects) are
retrieved without transformation.
\ttindex
{
im
_
func
}
\ttindex
{
im
_
self
}
\item
[Built-in functions]
A built-in function object is a wrapper around a
\C
{}
function. Examples
of built-in functions are
\function
{
len()
}
and
\function
{
math.sin()
}
...
...
@@ -482,7 +488,7 @@ returned. This implies a call to the class's \method{__init__()} method
if it has one. Any arguments are passed on to the
\method
{__
init
__
()
}
method. If there is no
\method
{__
init
__
()
}
method, the class must be called
without arguments.
\
ttindex
{__
init
__
}
\
withsubitem
{
(object method)
}{
\ttindex
{__
init
__
()
}
}
\obindex
{
class
}
\obindex
{
class instance
}
\obindex
{
instance
}
...
...
@@ -514,7 +520,7 @@ e.g., \samp{m.x = 1} is equivalent to \samp{m.__dict__["x"] = 1}.
Special read-only attribute:
\member
{__
dict
__}
is the module's
namespace as a dictionary object.
\
ttindex
{__
dict
__
}
\
withsubitem
{
(module attribute)
}{
\ttindex
{__
dict
__}
}
Predefined (writable) attributes:
\member
{__
name
__}
is the module's name;
\member
{__
doc
__}
is the
...
...
@@ -525,9 +531,10 @@ The \member{__file__} attribute is not present for C{} modules that are
statically linked into the interpreter; for extension modules loaded
dynamically from a shared library, it is the pathname of the shared
library file.
\ttindex
{__
name
__}
\ttindex
{__
doc
__}
\ttindex
{__
file
__}
\withsubitem
{
(module attribute)
}{
%
\ttindex
{__
name
__}
%
\ttindex
{__
doc
__}
%
\ttindex
{__
file
__}}
\indexii
{
module
}{
namespace
}
\item
[Classes]
...
...
@@ -569,11 +576,12 @@ Special attributes: \member{__name__} is the class name;
containing the base classes, in the order of their occurrence in the
base class list;
\member
{__
doc
__}
is the class's documentation string,
or None if undefined.
\ttindex
{__
name
__}
\ttindex
{__
module
__}
\ttindex
{__
dict
__}
\ttindex
{__
bases
__}
\ttindex
{__
doc
__}
\withsubitem
{
(class attribute)
}{
%
\ttindex
{__
name
__}
%
\ttindex
{__
module
__}
%
\ttindex
{__
dict
__}
%
\ttindex
{__
bases
__}
%
\ttindex
{__
doc
__}}
\item
[Class instances]
A class instance is created by calling a class object (see above).
...
...
@@ -609,8 +617,9 @@ section \ref{specialnames}, ``Special method names.''
Special attributes:
\member
{__
dict
__}
is the attribute
dictionary;
\member
{__
class
__}
is the instance's class.
\ttindex
{__
dict
__}
\ttindex
{__
class
__}
\withsubitem
{
(instance attribute)
}{
%
\ttindex
{__
dict
__}
%
\ttindex
{__
class
__}}
\item
[Files]
A file object represents an open file. File objects are created by the
...
...
@@ -626,11 +635,12 @@ Library Reference} for complete documentation of file objects.
\indexii
{
C
}{
language
}
\index
{
stdio
}
\bifuncindex
{
open
}
\bifuncindex
{
popen
}
\bifuncindex
{
makefile
}
\ttindex
{
stdin
}
\ttindex
{
stdout
}
\ttindex
{
stderr
}
\withsubitem
{
(in module os)
}{
\ttindex
{
popen()
}}
\withsubitem
{
(socket method)
}{
\ttindex
{
makefile()
}}
\withsubitem
{
(in module sys)
}{
%
\ttindex
{
stdin
}
%
\ttindex
{
stdout
}
%
\ttindex
{
stderr
}}
\ttindex
{
sys.stdin
}
\ttindex
{
sys.stdout
}
\ttindex
{
sys.stderr
}
...
...
@@ -658,26 +668,35 @@ contain no references (directly or indirectly) to mutable objects.
\index
{
bytecode
}
\obindex
{
code
}
Special read-only attributes:
\member
{
co
_
name
}
\ttindex
{
co
_
name
}
gives
the function name;
\member
{
co
_
argcount
}
\ttindex
{
co
_
argcount
}
is the number of positional arguments (including arguments with
default values);
\member
{
co
_
nlocals
}
\ttindex
{
co
_
nlocals
}
is the number
of local variables used by the function (including arguments);
\member
{
co
_
varnames
}
\ttindex
{
co
_
varnames
}
is a tuple containing the
names of the local variables (starting with the argument names);
\member
{
co
_
code
}
\ttindex
{
co
_
code
}
is a string representing the sequence
of bytecode instructions;
\member
{
co
_
consts
}
\ttindex
{
co
_
consts
}
is a
tuple containing the literals used by the bytecode;
\member
{
co
_
names
}
\ttindex
{
co
_
names
}
is a tuple containing the names used
by the bytecode;
\member
{
co
_
filename
}
\ttindex
{
co
_
filename
}
is the
filename from which the code was compiled;
\member
{
co
_
firstlineno
}
\ttindex
{
co
_
firstlineno
}
is the first line number
of the function;
\member
{
co
_
lnotab
}
\ttindex
{
co
_
lnotab
}
is a string
encoding the mapping from byte code offsets to line numbers (for
detais see the source code of the interpreter);
\member
{
co
_
stacksize
}
\ttindex
{
co
_
stacksize
}
is the required stack size
(including local variables);
\member
{
co
_
flags
}
\ttindex
{
co
_
flags
}
is an
integer encoding a number of flags for the interpreter.
Special read-only attributes:
\member
{
co
_
name
}
gives the function
name;
\member
{
co
_
argcount
}
is the number of positional arguments
(including arguments with default values);
\member
{
co
_
nlocals
}
is the
number of local variables used by the function (including arguments);
\member
{
co
_
varnames
}
is a tuple containing the names of the local
variables (starting with the argument names);
\member
{
co
_
code
}
is a
string representing the sequence of bytecode instructions;
\member
{
co
_
consts
}
is a tuple containing the literals used by the
bytecode;
\member
{
co
_
names
}
is a tuple containing the names used by
the bytecode;
\member
{
co
_
filename
}
is the filename from which the code
was compiled;
\member
{
co
_
firstlineno
}
is the first line number of the
function;
\member
{
co
_
lnotab
}
is a string encoding the mapping from
byte code offsets to line numbers (for detais see the source code of
the interpreter);
\member
{
co
_
stacksize
}
is the required stack size
(including local variables);
\member
{
co
_
flags
}
is an integer encoding
a number of flags for the interpreter.
\withsubitem
{
(code object attribute)
}{
%
\ttindex
{
co
_
argcount
}
%
\ttindex
{
co
_
code
}
%
\ttindex
{
co
_
consts
}
%
\ttindex
{
co
_
filename
}
%
\ttindex
{
co
_
firstlineno
}
%
\ttindex
{
co
_
flags
}
%
\ttindex
{
co
_
lnotab
}
%
\ttindex
{
co
_
name
}
%
\ttindex
{
co
_
names
}
%
\ttindex
{
co
_
nlocals
}
%
\ttindex
{
co
_
stacksize
}
%
\ttindex
{
co
_
varnames
}}
The following flag bits are defined for
\member
{
co
_
flags
}
: bit 2 is set
if the function uses the
\samp
{
*arguments
}
syntax to accept an
...
...
@@ -687,6 +706,7 @@ arguments; other bits are used internally or reserved for future use.
If a code object represents a function, the first item in
\member
{
co
_
consts
}
is the documentation string of the
function, or
\code
{
None
}
if undefined.
\index
{
documentation string
}
\item
[Frame objects]
Frame objects represent execution frames. They may occur in traceback
...
...
@@ -704,24 +724,26 @@ executing in restricted execution mode;
\member
{
f
_
lineno
}
gives the line number and
\member
{
f
_
lasti
}
gives the
precise instruction (this is an index into the bytecode string of
the code object).
\ttindex
{
f
_
back
}
\ttindex
{
f
_
code
}
\ttindex
{
f
_
globals
}
\ttindex
{
f
_
locals
}
\ttindex
{
f
_
lineno
}
\ttindex
{
f
_
lasti
}
\ttindex
{
f
_
builtins
}
\ttindex
{
f
_
restricted
}
\withsubitem
{
(frame attribute)
}{
%
\ttindex
{
f
_
back
}
%
\ttindex
{
f
_
code
}
%
\ttindex
{
f
_
globals
}
%
\ttindex
{
f
_
locals
}
%
\ttindex
{
f
_
lineno
}
%
\ttindex
{
f
_
lasti
}
%
\ttindex
{
f
_
builtins
}
%
\ttindex
{
f
_
restricted
}}
Special writable attributes:
\member
{
f
_
trace
}
, if not
\code
{
None
}
, is a
function called at the start of each source code line (this is used by
the debugger);
\member
{
f
_
exc
_
type
}
,
\member
{
f
_
exc
_
value
}
,
\member
{
f
_
exc
_
traceback
}
represent the most recent exception caught in
this frame.
\ttindex
{
f
_
trace
}
\ttindex
{
f
_
exc
_
type
}
\ttindex
{
f
_
exc
_
value
}
\ttindex
{
f
_
exc
_
traceback
}
\withsubitem
{
(frame attribute)
}{
%
\ttindex
{
f
_
trace
}
%
\ttindex
{
f
_
exc
_
type
}
%
\ttindex
{
f
_
exc
_
value
}
%
\ttindex
{
f
_
exc
_
traceback
}}
\item
[Traceback objects]
\label
{
traceback
}
Traceback objects represent a stack trace of an exception. A
...
...
@@ -743,9 +765,10 @@ interactive, it is also made available to the user as
\indexii
{
stack
}{
trace
}
\indexii
{
exception
}{
handler
}
\indexii
{
execution
}{
stack
}
\ttindex
{
exc
_
info
}
\ttindex
{
exc
_
traceback
}
\ttindex
{
last
_
traceback
}
\withsubitem
{
(in module sys)
}{
%
\ttindex
{
exc
_
info
}
%
\ttindex
{
exc
_
traceback
}
%
\ttindex
{
last
_
traceback
}}
\ttindex
{
sys.exc
_
info
}
\ttindex
{
sys.exc
_
traceback
}
\ttindex
{
sys.last
_
traceback
}
...
...
@@ -759,10 +782,11 @@ precise instruction. The line number and last instruction in the
traceback may differ from the line number of its frame object if the
exception occurred in a
\keyword
{
try
}
statement with no matching
except clause or with a finally clause.
\ttindex
{
tb
_
next
}
\ttindex
{
tb
_
frame
}
\ttindex
{
tb
_
lineno
}
\ttindex
{
tb
_
lasti
}
\withsubitem
{
(traceback attribute)
}{
%
\ttindex
{
tb
_
next
}
%
\ttindex
{
tb
_
frame
}
%
\ttindex
{
tb
_
lineno
}
%
\ttindex
{
tb
_
lasti
}}
\stindex
{
try
}
\item
[Slice objects]
...
...
@@ -770,11 +794,15 @@ Slice objects are used to represent slices when \emph{extended slice
syntax
}
is used. This is a slice using two colons, or multiple slices
or ellipses separated by commas, e.g.,
\code
{
a[i:j:step]
}
,
\code
{
a[i:j,
k:l]
}
, or
\code
{
a[..., i:j])
}
. They are also created by the built-in
\function
{
slice()
}
function.
\function
{
slice()
}
\bifuncindex
{
slice
}
function.
Special read-only attributes:
\member
{
start
}
is the lowerbound;
\member
{
stop
}
is the upperbound;
\member
{
step
}
is the step value; each is
\code
{
None
}
if omitted. These attributes can have any type.
\withsubitem
{
(slice object attribute)
}{
%
\ttindex
{
start
}
%
\ttindex
{
stop
}
%
\ttindex
{
step
}}
\end{description}
% Internal types
...
...
@@ -792,24 +820,23 @@ this class, then \code{x[i]} is equivalent to
a list object,
\code
{
x.
__
getitem
__
(i)
}
is not equivalent to
\code
{
x[i]
}
.) Except where mentioned, attempts to execute an
operation raise an exception when no appropriate method is defined.
\
ttindex
{__
getitem
__
}
\
withsubitem
{
(mapping object method)
}{
\ttindex
{__
getitem
__
()
}
}
\subsection
{
Basic customization
\label
{
customization
}}
\begin{methoddesc
ni}
{__
init
__}{
self
\optional
{
, args...
}}
\begin{methoddesc
}
[object]
{__
init
__}{
self
\optional
{
, args...
}}
Called when the instance is created. The arguments are those passed
to the class constructor expression. If a base class has an
\method
{__
init
__
()
}
method the derived class's
\method
{__
init
__
()
}
method must
explicitly call it to ensure proper initialization of the base class
part of the instance, e.g.,
\samp
{
BaseClass.
__
init
__
(
\var
{
self
}
,
[
\var
{
args
}
...])
}
.
\ttindex
{__
init
__}
\indexii
{
class
}{
constructor
}
\end{methoddesc
ni
}
\end{methoddesc}
\begin{methoddesc
ni}
{__
del
__}{
self
}
\begin{methoddesc
}
[object]
{__
del
__}{
self
}
Called when the instance is about to be destroyed. This is also
called a destructor
\index
{
destructor
}
. If a base class
has a
\method
{__
del
__
()
}
method, the derived class's
\method
{__
del
__
()
}
method
...
...
@@ -821,7 +848,6 @@ reference to it. It may then be called at a later time when this new
reference is deleted. It is not guaranteed that
\method
{__
del
__
()
}
methods are called for objects that still exist when
the interpreter exits.
\ttindex
{__
del
__}
\stindex
{
del
}
\strong
{
Programmer's note:
}
\samp
{
del x
}
doesn't directly call
...
...
@@ -853,9 +879,9 @@ deleted from their module before other globals are deleted; if no
other references to such globals exist, this may help in assuring that
imported modules are still available at the time when the
\method
{__
del
__
()
}
method is called.
\end{methoddesc
ni
}
\end{methoddesc}
\begin{methoddesc
ni}
{__
repr
__}{
self
}
\begin{methoddesc
}
[object]
{__
repr
__}{
self
}
Called by the
\function
{
repr()
}
\bifuncindex
{
repr
}
built-in function
and by string conversions (reverse quotes) to compute the ``official''
string representation of an object. This should normally look like a
...
...
@@ -864,36 +890,33 @@ the same value. By convention, objects which cannot be trivially
converted to strings which can be used to create a similar object
produce a string of the form
\samp
{
<
\var
{
...some useful
description...
}
>
}
.
\ttindex
{__
repr
__}
\indexii
{
string
}{
conversion
}
\indexii
{
reverse
}{
quotes
}
\indexii
{
backward
}{
quotes
}
\index
{
back-quotes
}
\end{methoddesc
ni
}
\end{methoddesc}
\begin{methoddesc
ni}
{__
str
__}{
self
}
\begin{methoddesc
}
[object]
{__
str
__}{
self
}
Called by the
\function
{
str()
}
\bifuncindex
{
str
}
built-in function and
by the
\keyword
{
print
}
\stindex
{
print
}
statement to compute the
``informal'' string representation of an object. This differs from
\method
{__
repr
__
()
}
in that it does not have to be a valid Python
expression: a more convenient or concise representation may be used
instead.
\ttindex
{__
str
__}
\end{methoddescni}
\end{methoddesc}
\begin{methoddesc
ni}
{__
cmp
__}{
self, other
}
\begin{methoddesc
}
[object]
{__
cmp
__}{
self, other
}
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
}
. If no
\method
{__
cmp
__
()
}
operation is defined, class
instances are compared by object identity (``address'').
(Note: the restriction that exceptions are not propagated by
\method
{__
cmp
__
()
}
has been removed in Python 1.5.)
\ttindex
{__
cmp
__}
\bifuncindex
{
cmp
}
\index
{
comparisons
}
\end{methoddesc
ni
}
\end{methoddesc}
\begin{methoddesc
ni}
{__
hash
__}{
self
}
\begin{methoddesc
}
[object]
{__
hash
__}{
self
}
Called for the key object for dictionary
\obindex
{
dictionary
}
operations, and by the built-in function
\function
{
hash()
}
\bifuncindex
{
hash
}
. Should return a 32-bit integer
...
...
@@ -910,18 +933,17 @@ implements a \method{__cmp__()} method it should not implement
\method
{__
hash
__
()
}
, since the dictionary implementation requires that
a key's hash value is immutable (if the object's hash value changes, it
will be in the wrong hash bucket).
\ttindex
{__
cmp
__}
\ttindex
{__
hash
__}
\end{methoddescni}
\withsubitem
{
(object method)
}{
\ttindex
{__
cmp
__
()
}}
\end{methoddesc}
\begin{methoddesc
ni}
{__
nonzero
__}{
self
}
\begin{methoddesc
}
[object]
{__
nonzero
__}{
self
}
Called to implement truth value testing; should return
\code
{
0
}
or
\code
{
1
}
. When this method is not defined,
\method
{__
len
__
()
}
is
called, if it is defined (see below). If a class defines neither
\method
{__
len
__
()
}
nor
\method
{__
nonzero
__
()
}
, all its instances are
considered true.
\
ttindex
{__
nonzero
__
}
\end{methoddesc
ni
}
\
withsubitem
{
(mapping object method)
}{
\ttindex
{__
len
__
()
}
}
\end{methoddesc}
\subsection
{
Customizing attribute access
\label
{
attribute-access
}}
...
...
@@ -933,13 +955,12 @@ For performance reasons, these methods are cached in the class object
at class definition time; therefore, they cannot be changed after the
class definition is executed.
\begin{methoddesc
ni}
{__
getattr
__}{
self, name
}
\begin{methoddesc
}
[object]
{__
getattr
__}{
self, name
}
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
the class tree for
\code
{
self
}
).
\code
{
name
}
is the attribute name.
This method should return the (computed) attribute value or raise an
\exception
{
AttributeError
}
exception.
\ttindex
{__
getattr
__}
Note that if the attribute is found through the normal mechanism,
\method
{__
getattr
__
()
}
is not called. (This is an intentional
...
...
@@ -950,40 +971,38 @@ the instance.
Note that at least for instance variables, you can fake
total control by not inserting any values in the instance
attribute dictionary (but instead inserting them in another object).
\
ttindex
{__
setattr
__
}
\end{methoddesc
ni
}
\
withsubitem
{
(object method)
}{
\ttindex
{__
setattr
__
()
}
}
\end{methoddesc}
\begin{methoddesc
ni}
{__
setattr
__}{
self, name, value
}
\begin{methoddesc
}
[object]
{__
setattr
__}{
self, name, value
}
Called when an attribute assignment is attempted. This is called
instead of the normal mechanism (i.e.
\
store the value in the instance
dictionary).
\var
{
name
}
is the attribute name,
\var
{
value
}
is the
value to be assigned to it.
\ttindex
{__
setattr
__}
If
\method
{__
setattr
__
()
}
wants to assign to an instance attribute, it
should not simply execute
\samp
{
self.
\var
{
name
}
= value
}
--- this
would cause a recursive call to itself. Instead, it should insert the
value in the dictionary of instance attributes, e.g.,
\samp
{
self.
__
dict
__
[
\var
{
name
}
] = value
}
.
\
ttindex
{__
dict
__
}
\end{methoddesc
ni
}
\
withsubitem
{
(instance attribute)
}{
\ttindex
{__
dict
__}
}
\end{methoddesc}
\begin{methoddesc
ni}
{__
delattr
__}{
self, name
}
\begin{methoddesc
}
[object]
{__
delattr
__}{
self, name
}
Like
\method
{__
setattr
__
()
}
but for attribute deletion instead of
assignment.
\ttindex
{__
delattr
__}
\end{methoddesc
ni
}
assignment.
This should only be implemented if
\samp
{
del
obj.
\var
{
name
}}
is meaningful for the object.
\end{methoddesc}
\subsection
{
Emulating callable objects
\label
{
callable-types
}}
\begin{methoddesc
ni}
{__
call
__}{
self
\optional
{
, args...
}}
\begin{methoddesc
}
[object]
{__
call
__}{
self
\optional
{
, args...
}}
Called when the instance is ``called'' as a function; if this method
is defined,
\code
{
\var
{
x
}
(arg1, arg2, ...)
}
is a shorthand for
\code
{
\var
{
x
}
.
__
call
__
(arg1, arg2, ...)
}
.
\ttindex
{__
call
__}
\indexii
{
call
}{
instance
}
\end{methoddesc
ni
}
\end{methoddesc}
\subsection
{
Emulating sequence and mapping types
\label
{
sequence-types
}}
...
...
@@ -1007,57 +1026,60 @@ multiplication (meaning repetition) by defining the methods
\method
{__
add
__
()
}
,
\method
{__
radd
__
()
}
,
\method
{__
mul
__
()
}
and
\method
{__
rmul
__
()
}
described below; they should not define
\method
{__
coerce
__
()
}
or other numerical operators.
\ttindex
{
keys
}
\ttindex
{
values
}
\ttindex
{
items
}
\ttindex
{
has
_
key
}
\ttindex
{
get
}
\ttindex
{
clear
}
\ttindex
{
copy
}
\ttindex
{
update
}
\ttindex
{
append
}
\ttindex
{
count
}
\ttindex
{
index
}
\ttindex
{
insert
}
\ttindex
{
pop
}
\ttindex
{
remove
}
\ttindex
{
reverse
}
\ttindex
{
sort
}
\ttindex
{__
add
__}
\ttindex
{__
radd
__}
\ttindex
{__
mul
__}
\ttindex
{__
rmul
__}
\ttindex
{__
coerce
__}
\begin{methoddescni}
{__
len
__}{
self
}
\withsubitem
{
(mapping object method)
}{
%
\ttindex
{
keys()
}
%
\ttindex
{
values()
}
%
\ttindex
{
items()
}
%
\ttindex
{
has
_
key()
}
%
\ttindex
{
get()
}
%
\ttindex
{
clear()
}
%
\ttindex
{
copy()
}
%
\ttindex
{
update()
}}
\withsubitem
{
(sequence object method)
}{
%
\ttindex
{
append()
}
%
\ttindex
{
count()
}
%
\ttindex
{
index()
}
%
\ttindex
{
insert()
}
%
\ttindex
{
pop()
}
%
\ttindex
{
remove()
}
%
\ttindex
{
reverse()
}
%
\ttindex
{
sort()
}
%
\ttindex
{__
add
__
()
}
%
\ttindex
{__
radd
__
()
}
%
\ttindex
{__
mul
__
()
}
%
\ttindex
{__
rmul
__
()
}}
\withsubitem
{
(numberic object method)
}{
\ttindex
{__
coerce
__
()
}}
\begin{methoddesc}
[mapping object]
{__
len
__}{
self
}
Called to implement the built-in function
\function
{
len()
}
\bifuncindex
{
len
}
. Should return the length of the
object, an integer
\code
{
>=
}
0. Also, an object that doesn't define a
\method
{__
nonzero
__
()
}
method and whose
\method
{__
len
__
()
}
method
returns zero is considered to be false in a Boolean context.
\ttindex
{__
len
__}
\ttindex
{__
nonzero
__}
\end{methoddescni}
\withsubitem
{
(object method)
}{
\ttindex
{__
nonzero
__
()
}}
\end{methoddesc}
\begin{methoddesc
ni}
{__
getitem
__}{
self, key
}
\begin{methoddesc
}
[mapping object]
{__
getitem
__}{
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
special interpretation of negative indices (if the class wishes to
emulate a sequence type) is up to the
\method
{__
getitem
__
()
}
method.
\ttindex
{__
getitem
__}
\end{methoddescni}
\end{methoddesc}
\begin{methoddesc
ni}
{__
setitem
__}{
self, key, value
}
\begin{methoddesc
}
[mapping object]
{__
setitem
__}{
self, key, value
}
Called to implement assignment to
\code
{
\var
{
self
}
[
\var
{
key
}
]
}
. Same
note as for
\method
{__
getitem
__
()
}
.
\ttindex
{__
setitem
__}
\end{methoddescni}
note as for
\method
{__
getitem
__
()
}
. This should only be implemented
for mappings if the objects support changes to the values for keys, or
if new keys can be added, or for sequences if elements can be
replaced.
\end{methoddesc}
\begin{methoddesc
ni}
{__
delitem
__}{
self, key
}
\begin{methoddesc
}
[mapping object]
{__
delitem
__}{
self, key
}
Called to implement deletion of
\code
{
\var
{
self
}
[
\var
{
key
}
]
}
. Same
note as for
\method
{__
getitem
__
()
}
.
\ttindex
{__
delitem
__}
\end{methoddescni}
note as for
\method
{__
getitem
__
()
}
. This should only be implemented
for mappings if the objects support removal of keys, or for sequences
if elements can be removed from the sequence.
\end{methoddesc}
\subsection
{
Additional methods for emulation of sequence types
...
...
@@ -1068,7 +1090,7 @@ objects. Immutable sequences methods should only define
\method
{__
getslice
__
()
}
; mutable sequences, should define all three
three methods.
\begin{methoddesc
ni}
{__
getslice
__}{
self, i, j
}
\begin{methoddesc
}
[sequence object]
{__
getslice
__}{
self, i, j
}
Called to implement evaluation of
\code
{
\var
{
self
}
[
\var
{
i
}
:
\var
{
j
}
]
}
.
The returned object should be of the same type as
\var
{
self
}
. Note
that missing
\var
{
i
}
or
\var
{
j
}
in the slice expression are replaced
...
...
@@ -1076,20 +1098,17 @@ by zero or \code{sys.maxint}, respectively, and no further
transformations on the indices is performed. The interpretation of
negative indices and indices larger than the length of the sequence is
up to the method.
\ttindex
{__
getslice
__}
\end{methoddescni}
\end{methoddesc}
\begin{methoddesc
ni}
{__
setslice
__}{
self, i, j, sequence
}
\begin{methoddesc
}
[sequence object]
{__
setslice
__}{
self, i, j, sequence
}
Called to implement assignment to
\code
{
\var
{
self
}
[
\var
{
i
}
:
\var
{
j
}
]
}
.
Same notes for
\var
{
i
}
and
\var
{
j
}
as for
\method
{__
getslice
__
()
}
.
\ttindex
{__
setslice
__}
\end{methoddescni}
\end{methoddesc}
\begin{methoddesc
ni}
{__
delslice
__}{
self, i, j
}
\begin{methoddesc
}
[sequence object]
{__
delslice
__}{
self, i, j
}
Called to implement deletion of
\code
{
\var
{
self
}
[
\var
{
i
}
:
\var
{
j
}
]
}
.
Same notes for
\var
{
i
}
and
\var
{
j
}
as for
\method
{__
getslice
__
()
}
.
\ttindex
{__
delslice
__}
\end{methoddescni}
\end{methoddesc}
Notice that these methods are only invoked when a single slice with a
single colon is used. For slice operations involving extended slice
...
...
@@ -1103,18 +1122,18 @@ Methods corresponding to operations that are not supported by the
particular kind of number implemented (e.g., bitwise operations for
non-integral numbers) should be left undefined.
\begin{methoddesc
ni}
{__
add
__}{
self, other
}
\methodline
ni
{__
sub
__}{
self, other
}
\methodline
ni
{__
mul
__}{
self, other
}
\methodline
ni
{__
div
__}{
self, other
}
\methodline
ni
{__
mod
__}{
self, other
}
\methodline
ni
{__
divmod
__}{
self, other
}
\methodline
ni
{__
pow
__}{
self, other
\optional
{
, modulo
}}
\methodline
ni
{__
lshift
__}{
self, other
}
\methodline
ni
{__
rshift
__}{
self, other
}
\methodline
ni
{__
and
__}{
self, other
}
\methodline
ni
{__
xor
__}{
self, other
}
\methodline
ni
{__
or
__}{
self, other
}
\begin{methoddesc
}
[numberic interface]
{__
add
__}{
self, other
}
\methodline
{__
sub
__}{
self, other
}
\methodline
{__
mul
__}{
self, other
}
\methodline
{__
div
__}{
self, other
}
\methodline
{__
mod
__}{
self, other
}
\methodline
{__
divmod
__}{
self, other
}
\methodline
{__
pow
__}{
self, other
\optional
{
, modulo
}}
\methodline
{__
lshift
__}{
self, other
}
\methodline
{__
rshift
__}{
self, other
}
\methodline
{__
and
__}{
self, other
}
\methodline
{__
xor
__}{
self, other
}
\methodline
{__
or
__}{
self, other
}
These functions are
called to implement the binary arithmetic operations (
\code
{
+
}
,
\code
{
-
}
,
\code
{
*
}
,
\code
{
/
}
,
\code
{
\%
}
,
...
...
@@ -1127,32 +1146,20 @@ class that has an \method{__add__()} method,
\method
{__
pow
__
()
}
should be defined to accept an optional third
argument if the ternary version of the built-in
\function
{
pow()
}
\bifuncindex
{
pow
}
function is to be supported.
\ttindex
{__
or
__}
\ttindex
{__
xor
__}
\ttindex
{__
and
__}
\ttindex
{__
rshift
__}
\ttindex
{__
lshift
__}
\ttindex
{__
pow
__}
\ttindex
{__
divmod
__}
\ttindex
{__
mod
__}
\ttindex
{__
div
__}
\ttindex
{__
mul
__}
\ttindex
{__
sub
__}
\ttindex
{__
add
__}
\end{methoddescni}
\begin{methoddescni}
{__
radd
__}{
self, other
}
\methodlineni
{__
rsub
__}{
self, other
}
\methodlineni
{__
rmul
__}{
self, other
}
\methodlineni
{__
rdiv
__}{
self, other
}
\methodlineni
{__
rmod
__}{
self, other
}
\methodlineni
{__
rdivmod
__}{
self, other
}
\methodlineni
{__
rpow
__}{
self, other
}
\methodlineni
{__
rlshift
__}{
self, other
}
\methodlineni
{__
rrshift
__}{
self, other
}
\methodlineni
{__
rand
__}{
self, other
}
\methodlineni
{__
rxor
__}{
self, other
}
\methodlineni
{__
ror
__}{
self, other
}
\end{methoddesc}
\begin{methoddesc}
[numeric interface]
{__
radd
__}{
self, other
}
\methodline
{__
rsub
__}{
self, other
}
\methodline
{__
rmul
__}{
self, other
}
\methodline
{__
rdiv
__}{
self, other
}
\methodline
{__
rmod
__}{
self, other
}
\methodline
{__
rdivmod
__}{
self, other
}
\methodline
{__
rpow
__}{
self, other
}
\methodline
{__
rlshift
__}{
self, other
}
\methodline
{__
rrshift
__}{
self, other
}
\methodline
{__
rand
__}{
self, other
}
\methodline
{__
rxor
__}{
self, other
}
\methodline
{__
ror
__}{
self, other
}
These functions are
called to implement the binary arithmetic operations (
\code
{
+
}
,
\code
{
-
}
,
\code
{
*
}
,
\code
{
/
}
,
\code
{
\%
}
,
...
...
@@ -1166,55 +1173,33 @@ has an \method{__rsub__()} method, \code{\var{y}.__rsub__(\var{x})} is
called. Note that ternary
\function
{
pow()
}
\bifuncindex
{
pow
}
will not
try calling
\method
{__
rpow
__
()
}
(the coercion rules would become too
complicated).
\ttindex
{__
or
__}
\ttindex
{__
xor
__}
\ttindex
{__
and
__}
\ttindex
{__
rshift
__}
\ttindex
{__
lshift
__}
\ttindex
{__
pow
__}
\ttindex
{__
divmod
__}
\ttindex
{__
mod
__}
\ttindex
{__
div
__}
\ttindex
{__
mul
__}
\ttindex
{__
sub
__}
\ttindex
{__
add
__}
\end{methoddescni}
\begin{methoddescni}
{__
neg
__}{
self
}
\methodlineni
{__
pos
__}{
self
}
\methodlineni
{__
abs
__}{
self
}
\methodlineni
{__
invert
__}{
self
}
\end{methoddesc}
\begin{methoddesc}
[numeric interface]
{__
neg
__}{
self
}
\methodline
{__
pos
__}{
self
}
\methodline
{__
abs
__}{
self
}
\methodline
{__
invert
__}{
self
}
Called to implement the unary arithmetic operations (
\code
{
-
}
,
\code
{
+
}
,
\function
{
abs()
}
\bifuncindex
{
abs
}
and
\code
{
~
}
).
\ttindex
{__
invert
__}
\ttindex
{__
abs
__}
\ttindex
{__
pos
__}
\ttindex
{__
neg
__}
\end{methoddescni}
\end{methoddesc}
\begin{methoddesc
ni}
{__
int
__}{
self
}
\begin{methoddesc
}
[numeric interface]
{__
int
__}{
self
}
\methodlineni
{__
long
__}{
self
}
\methodlineni
{__
float
__}{
self
}
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
{__
long
__}
\ttindex
{__
int
__}
\end{methoddescni}
\end{methoddesc}
\begin{methoddesc
ni}
{__
oct
__}{
self
}
\begin{methoddesc
}
[numeric interface]
{__
oct
__}{
self
}
\methodlineni
{__
hex
__}{
self
}
Called to implement the built-in functions
\function
{
oct()
}
\bifuncindex
{
oct
}
and
\function
{
hex()
}
\bifuncindex
{
hex
}
. Should return a string value.
\ttindex
{__
hex
__}
\ttindex
{__
oct
__}
\end{methoddescni}
\end{methoddesc}
\begin{methoddescni}
{__
coerce
__}{
self, other
}
\ttindex
{__
coerce
__}
\begin{methoddesc}
[numeric interface]
{__
coerce
__}{
self, other
}
Called to implement ``mixed-mode'' numeric arithmetic. Should either
return a 2-tuple containing
\var
{
self
}
and
\var
{
other
}
converted to
a common numeric type, or
\code
{
None
}
if conversion is possible. When
...
...
@@ -1223,7 +1208,7 @@ return \code{None}, since the interpreter will also ask the other
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 here).
\end{methoddesc
ni
}
\end{methoddesc}
\strong
{
Coercion rules
}
: to evaluate
\var
{
x
}
\var
{
op
}
\var
{
y
}
, the
following steps are taken (where
\method
{__
op
__
()
}
and
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment