Next / Previous / Contents / TCC Help System / NM Tech homepage

17.2. Special method names

Below are most of the commonly used special methods you can define in your class. Refer to the Python Reference Manual for a complete list.

.__abs__(self)

Defines the behavior of the abs() function when applied to an object of your class.

.__add__(self,other)

Defines the behavior of this class for self+other.

.__and__(self,other)

Defines the behavior of self&other.

.__cmp__(self,other)

Compares two values, self and other, to see which way they should be ordered. Return a negative integer if self should come first; return a positive integer if the other value should come first; and return an integer 0 if they are considered equal.

.__complex__(self)

Defines the behavior of the builtin function complex(), to convert self to a complex value.

.__contains__(self,x)

You can use this special method to define how the “in” and “not in” operators work to test whether an object x is a member of self. The method returns true if x is in self, false otherwise.

.__del__(self)

Destructor: this method is called when the instance is about to be destroyed because there are no more references to it.

.__delitem__(self,key)

Called to delete self[key]. Raise IndexError if the key is not valid.

.__div__(self,other)

Defines the behavior of self/other.

.__divmod__(self,other)

Defines the behavior of divmod(self,other).

.__float__(self)

Defines the behavior of the builtin float() function; should return a float value.

.__getattr__(self,name)

Get self's attribute whose name is the string name. If the name is not a legal attribute name, this method should raise AttributeError. This method is called only if the object doesn't have a regular attribute by the given name.

.__getitem__(self,key)

Called on access to self[key]. Raise IndexError if the key is not valid.

.__int__(self)

Defines the behavior of the builtin int() function; should return a value of type int.

.__invert__(self)

Defines the behavior of the unary ~ operator.

.__iter__(self)

If self is a container object, return an iterator that iterates over all the items in that object. See iterators below. For mapping objects, the iterator should iterate over all the keys.

.__hex__(self)

Defines the behavior of the builtin hex() function; should return a string of hexadecimal characters.

.__len__(self)

Return the length of self as a nonnegative integer.

.__long__(self)

Defines the behavior of the builtin long() function.

.__lshift__(self,other)

Defines the behavior of self<<other.

.__mod__(self,other)

Defines the behavior of self%other.

.__mul__(self,other)

Defines the behavior of self*other.

.__neg__(self)

Implements the unary negate operator -self.

.__nonzero__(self)

Called to test an object's truth value. Return 1 for true, 0 for false. If you don't define this method, Python tries computing the length with the .__len__() method; zero length is considered false and all other values true.

.__oct__(self)

Defines the behavior of the builtin oct() function; should return a string of octal characters.

.__or__(self,other)

Defines the behavior of self|other.

.__pow__(self,other[,modulo])

Defines the behavior of the exponentiation operator self**other. It also implements the built-in function pow(), which takes an optional third argument modulo; see that function for details about the third argument..

.__rshift__(self,other)

Defines the behavior of self>>other.

.__setattr__(self,name,value)

Called for assignments of the form

O.name = value

If your method needs to store a value in the instance, don't just assign a value, because that will force .__setattr__() to be called again, creating an infinite loop. Instead, use an assignment of the form

O.__dict__[name] = value
.__setitem__(self,key,value)

Called for assignments to self[key].

.__str__(self)

Returns self in the form of a string. The built-in function str() and the print statement use this method if it is defined.

.__sub__(self,other)

Defines the behavior of self-other.

.__xor__(self,other)

Defines the behavior of self^other.