GIV

Appendix D

Special Class Methods in Python



Special class methods are an important feature of Python, making it an extremely flexible language.  All user-defined classes may provide implementations of these methods, which will be called or invoked whenever the appropriate action is called for by the Python interpreter.  You'll find detailed discussion of all of these methods in Hour 13, Special Class Methods.  What is listed here is intended only to jog your memory, not provide a detailed explanation.  In line with that, the methods are here listed in alphabetical order.  A listing by use can be found at the official Python Reference Manual:  http://www.python.org/doc/current/ref/index.html.  Each explanation is preceded with the general use of the method.

Note that in the methods below, when it says "return the result" or something like that, you need to create a new object to return.  For instance, x = x + y uses x and y as inputs, but creates a whole new object to return, which is assigned to x after the calculation is performed.

__abs__(self)
Numeric; return the absolute value of self (no sign).
__add__(self,other)
Numeric and sequence; add self to other, or concatenate self and other, and return the result.
__and__(self,other)
Numeric; return the result of bitwise and'ing (&) self with other.
__call__(self[,args])
Class; if it makes sense to treat your class as a function, implement this method; args are optional.
__cmp__(self,other)
Class, others;  called by all comparison operations.  Return -1 if self is < other, 0 if self and other are equal, and 1 if self is > than other.
__coerce__(self,other)
Numeric; called whenever self and other must be converted to a common type for some sort of arithmetic operation.  Usually, you convert other to an instance of your class, although you could convert your class to whatever type other is.  Return a tuple:  (self,other).
__complex__(self)
Numeric; if your class can be converted to a complex number, return that complex equivalent.
__del__(self)
Class; called when an instance of your class is about to be destroyed.  Lots of gotchas; see Hour 13 for details.
__delattr__(self,name)
Access; called when del object.name is called.
__delitem__(self,key)
Sequence and mapping; called when del object[key] is called.
__delslice__(self,i,j)
Sequence and mapping; called when del object[i:j] is called.
__div__(self,other)
Numeric; divide self by other and return the result.
__divmod__(self,other)
Numeric; divide self by other and return a tuple of the result and the remainder.
__float__(self)
Numeric; if your class can be converted to a floating-point number, return that floating-point equivalent. 
__getattr__(self,name)
Access; only called if object.attribute lookup fails.  Return the value of attribute or raise AttributeError.
__getitem__(self,key)
Sequence and mapping; called when a call to object[key] is made.  Keys are usually integers; negative index implementation must be done in this method if your class supports it.
__getslice__(self,i,j)
Sequence and mapping; called when a call to object[i:j] is made. I and j are integers, negative index implementation is up to this method.
__hash__(self)
Class; return a 32-bit value that can be used as a hash index.  Called by using your class object as a key for a dictionary, and by the built in hash() function.  If your class is mutable, do not implement the __hash__ method.
__hex__(self)
Numeric; return a string representing the hexadecimal equivalent of your class.
__init__(self[,args])
Class; called when an instance of your class is created.  Args optional, of course.
__int__(self)
Numeric; if your class can be converted to an integer, return that integer equivalent.
__invert__(self)
Numeric; return the result of a bitwise invert operation (~).
__len__(self)
Sequence and mapping;  called by built in function len().  Return the length of your class, however you want to define it.
__long__(self)
Numeric; if your class can be converted to a long integer, return that long integer equivalent.
__lshift__(self,other)
Numeric; return the result of performing a left-shift (<<) by other bits or units on your class, but only if it makes sense.
__mod__(self,other)
Numeric; divide self by other and return the remainder.
__mul__(self,other)
Numeric; multipy self times other and return the result.
__neg__(self)
Numeric; perform the equivalent of multiplying your class times -1 and return the result.
__nonzero__(self)
Class; return 0 or 1 for truth testing.  if <your instance>: expressions, and so on.
__oct__(self)
Numeric; return a string containing an octal representation of your class.
__or__(self,other)
Numeric; perform the equivalent of a bitwise or operation (|) on your class and return the result.
__pos__(self)
Numeric; perform the equivalent of multiplying your class times 1.  This is the identity operation.
__pow__(self,other[,modulo])
Numeric; perform the equivalent of exponentiating self to the other power.  If the modulo argument is provided, perform (self ** other) % modulo.
__radd__(self,other)
Numeric;  add self and other and return result.  Called for e.g. 1 + class instead of  class + 1.
__rand__(self,other)
Numeric; return the result of bitwise and'ing (&) self with other.  Called when left operand is not an instance of your class.
__rdiv__(self,other)
Numeric; divide self by other and return the result.  Called when left operand is not an instance of your class.
__rdivmod__(self,other)
Numeric; divide self by other and return a tuple of the result and the remainder.   Called when left operand is not an instance of your class.
__repr__(self)
Class;  called by the repr() built in function and string conversions (backquotes).   By convention, this is supposed to return a string that can be used later to recreate the class instance.
__rlshift__(self,other)
Numeric; return the result of performing a left-shift (<<) by other bits or units on your class, but only if it makes sense.   Called when left operand is not an instance of your class.
__rmod__(self,other)
Numeric; divide self by other and return the remainder.   Called when left operand is not an instance of your class.
__rmul__(self,other)
Numeric; multipy self times other and return the result.   Called when left operand is not an instance of your class.
__ror__(self,other)
Numeric; perform the equivalent of a bitwise or operation (|) on your class and return the result.   Called when left operand is not an instance of your class.
__rpow__(self,other)
Numeric; perform the equivalent of exponentiating self to the other power.   No r-equivalent to pow(self,other,modulo).  Called when left operand is not an instance of your class.
__rrshift__(self,other)
Numeric; return the result of performing a right-shift (>>) by other bits or units on your class, but only if it makes sense.   Called when left operand is not an instance of your class.
__rshift__(self,other)
Numeric; return the result of performing a right-shift (>>) by other bits or units on your class, but only if it makes sense.
__rsub__(self,other)
Numeric; subtract other from self and return the result.   Called when left operand is not an instance of your class.
__rxor__(self,other)
Numeric; perform the equivalent of a bitwise xor operation (^) on your class and return the result.   Called when left operand is not an instance of your class.
__setattr__(self,name,value)
Access; called when an attempt to assign object.attribute = value is made.  When implementing this, insert the value in the dictionary of instance attributes:  self.__dict__[name] = value.
__setitem__(self,key,value)
Sequence and mapping; called when an attempt to assign object[key] = value is made.  Keys are usually integers.
__setslice__(self,i,j,sequence)
Sequence and mapping; called when an attempt to assign object[i:j] = sequence is made.  Keys are usually integers.
__str__(self)
Class; called by the str() built in function and by the print statement (also the % formatting expression); should return a string, but it's not required to be an actual Python expression in the same way that the return value of __repr__  is supposed to be.
__sub__(self,other)
Numeric; subtract other from self and return the result.
__xor__(self,other)
Numeric; perform the equivalent of a bitwise xor operation (^) on your class and return the result.

Valid HTML 4.01 Transitional