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