Table B.13 represents the set of special methods that can be implemented to allow user-defined objects to take on behaviors and functionality of Python standard types.
Table B.13. Special Methods for Customizing Classes
Special Method | Description |
---|
Basic Customization |
C.__init__(self[, arg1, ...]) | Constructor (with any optional arguments) |
C.__new__(self[, arg1, ...])[a] | Constructor (with any optional arguments); usually used for setting up subclassing of immutable data types |
C.__del__(self) | Destructor |
C.__str__(self) | Printable string representation; str() built-in and print statement |
C.__repr__(self) | Evaluatable string representation; repr() built-in and '' operator |
C.__unicode__(self)[b] | Unicode string representation; unicode() built-in |
C.__call__(self, *args) | Denote callable instances |
C.__nonzero__(self) | Define False value for object; bool() built-in (as of 2.2) |
C.__len__(self) | "Length" (appropriate for class); len() built-in |
Object (Value) Comparison[c] |
C.__cmp__(self, obj) | Object comparison; cmp() built-in |
C.__lt__(self, obj) and C.__le__(self, obj) | Less than/less than or equal to; < and <= operators |
C.__gt__(self, obj) and C.__ge__(self, obj) | Greater than/greater than or equal to; > and >= operators |
C.__eq__(self, obj) and | Equal/not equal to; |
C.__ne__(self, obj) | ==,!= and <> operators |
Attributes |
C.__getattr__(self, attr) | Get attribute; getattr() built-in |
C.__setattr__(self, attr, val) | Set attribute; setattr() built-in |
C.__delattr__(self, attr) | Delete attribute; del statement |
C.__getattribute__(self, attr)[a] | Get attribute; getattr() built-in |
C.__get__(self, attr) | Get attribute; getattr() built-in |
C.__set__(self, attr, val) | Set attribute; setattr() built-in |
C.__delete__(self, attr) | Delete attribute; del statement |
Customizing Classes/Emulating Types |
Numeric Types: binary operators[d] | |
C.__*add__(self, obj) | Addition; + operator |
C.__*sub__(self, obj) | Subtraction; - operator |
C.__*mul__(self, obj) | Multiplication; * operator |
C.__*div__(self, obj) | Division; / operator |
C.__*TRuediv__(self, obj)[f] | True division; / operator |
C.__*floordiv__(self, obj)[e] | Floor division; // operator |
C.__*mod__(self, obj) | Modulo/remainder; % operator |
C.__*divmod__(self, obj) | Division and modulo; divmod() built-in |
C.__*pow__(self, obj[, mod]) | Exponentiation; pow() built-in; ** operator |
C.__*lshift__(self, obj) | Left shift; << operator |
C.__*rshift__(self, obj) | Right shift; >> operator |
C.__*and__(self, obj) | Bitwise AND; & operator |
C.__*or__(self, obj) | Bitwise OR; | operator |
C.__*xor__(self, obj) | Bitwise XOR; ^ operator |
Numeric Types: unary operators | |
C.__neg__(self) | Unary negation |
C.__pos__(self) | Unary no-change |
C.__abs__(self) | Absolute value; abs() built-in |
C.__invert__(self) | Bit inversion; ~ operator |
Numeric Types: numeric conversion | |
C.__complex__(self, com) | Convert to complex; complex() built-in |
C.__int__(self) | Convert to int; int() built-in |
C.__long__(self) | Convert to long; long() built-in |
C.__float__(self) | Convert to float; float() built-in |
Numeric Types: base representation (string) | |
C.__oct__(self) | Octal representation; oct() built-in |
C.__hex__(self) | Hexadecimal representation; hex() built-in |
Numeric Types: numeric coercion | |
C.__coerce__(self, num) | Coerce to same numeric type; coerce() built-in |
Sequence Types[d] | |
C.__len__(self) | Number of items in sequence |
C.__getitem__(self, ind) | Get single sequence element |
C.__setitem__(self, ind, val) | Set single sequence element |
C.__delitem__(self, ind) | Delete single sequence element |
C.__getslice__(self, ind1, ind2) | Get sequence slice |
C.__setslice__(self, i1, i2, val) | Get sequence slice |
C.__delslice__(self, ind1, ind2) | Delete sequence slice |
C.__contains__(self, val)[f] | Test sequence membership; in keyword |
C.__*add__(self, obj) | Concatenation; + operator |
Sequence Types[d] | |
C.__*mul__(self, obj) | Repetition; * operator |
C.__iter__(self)[e] | Create iterator class; iter() built-in |
Mapping Types | |
C.__len__(self) | Number of items in mapping |
C.__hash__(self) | Hash function value |
C.__getitem__(self, key) | Get value with given key |
C.__setitem__(self, key, val) | Set value with given key |
C.__delitem__(self, key) | Delete value with given key |