Files for 2.1b1 distribution.
[python/dscho.git] / Demo / classes / class.doc
blobfddc60bf7bad6ad3754ceb24c40244b037e9701f
1 New features of classes
2 =======================
4 A class can implement certain operations that are invoked by special
5 syntax (such as subscription or arithmetic operations) by defining
6 methods with special names.
9 Special methods for any type
10 ----------------------------
12 __repr__(self) --> string
14 Used by the print statement and conversions (reverse quotes) to
15 compute the string representation of an object.
17 __cmp__(self, other) --> int
19 Used by all comparison operations.  Should return -1 if self<other, 0
20 if self==other, +1 if self>other.  Due to limitations in the
21 interpreter, exceptions raised by comparisons are ignored, and the
22 objects will be considered equal in this case.
25 Special methods for sequence and mapping types
26 ----------------------------------------------
28 __len__(self) --> int
30 Used by the built-in function len().  Should return the length of the
31 object, which should be >= 0.  Also, an object whose __len__() method
32 returns 0 
34 __getitem__(self, key) --> value
36 Used to implement value = self[key].  Note that the special
37 interpretation of negative keys (if the class wishes to emulate a
38 sequence type) is up to the __getitem__ method.
40 __setitem__(self, key, value)
42 Used to implement self[key] = value.  Same note as for __getitem__.
44 __delitem__(self, key)
46 Used to implement del self[key].  Same note as for __getitem__.
49 Special methods for sequence types
50 ----------------------------------
52 __getslice__(self, i, j) --> sequence
54 Used to implement self[i:j].  Note that missing i or j are replaced by
55 0 or len(self), respectively, and len(self) has been added to negative
56 i or j.
58 __setslice__(self, i, j, sequence)
60 Used to implement self[i:j] = value.  Same note as for __getslice__.
62 __delslice__(self, i, j)
64 Used to implement del self[i:j].  Same note as for __getslice__.
67 Special methods for numeric types
68 ---------------------------------
70 __add__, __sub__, __mul__, __div__, __mod__, __divmod__, __pow__,
71 __lshift__, __rshift__, __and__, __xor__, __or__
73 Used to implement the binary arithmetic operations (divmod and pow are
74 called by built-in functions).  All have the call pattern
75 func(self, other) --> number.
77 __neg__, __pos__, __abs__, __invert__
79 Used to implement the unary arithmetic operations (-, +, abs and ~).
80 All have the call pattern func(self) --> number.
82 __nonzero__(self) --> int
84 Used to implement boolean testing.  An alternative name for this
85 method is __len__.
87 __coerce__(self, other) --> (self1, other1) or None
89 Used to implement "mixed-mode" numeric arithmetic.  Either return a
90 tuple containing self and other converted to some common type, or None
91 if no way of conversion is known.  When the common type would be the
92 type of other, it is sufficient to return None, since the interpreter
93 will also ask the other object to attempt a coercion (but sometimes,
94 if the implementation of the other type cannot be changed, it is
95 useful to do the conversion to the other type here).
97 __int__(self) --> int
98 __long__(self) --> long
99 __float__(self) --> float
101 Used to implement the built-in functions int(), long() and float().
104 Notes
105 -----
107 Except for __repr__ and __cmp__, when no appropriate method is
108 defined, attempts to execute the operation raise an exception.  For
109 __repr__ and __cmp__, the traditional interpretations are used
110 in this case.