Python Objects and Classes Reference

Quick reference to objects and classes in Python.

Class attributes
Function Does what
__dict__ Dictionary containing the class's namespace.
__doc__ Class documentation string or none, if undefined.
__name__ Class name.
__module__ Module name in which the class is defined. This attribute is "__main__" in interactive mode.
__bases__ A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.
Class functions
Function Does what
issubclass(sub, sup) Boolean function: returns True if the given subclass sub is a subclass of the superclass sup.
isinstance(obj, Class) Boolean function: returns True if obj is an instance of class Class or is an instance of a subclass of Class
Class methods
Method Does what Example
__init__ ( self [,args...] ) Constructor (with any optional arguments) obj = className(args)
__del__( self ) Destructor, deletes an object del obj
__repr__( self ) Evaluable string representation repr(obj)
__str__( self ) Printable string representation str(obj)
__cmp__ ( self, x ) Object comparison cmp(obj, x)
Object attributes
Function Does what
hasattr(obj, 'attr') Returns true if 'attr' attribute exists.
getattr(obj, 'attr') Returns value of 'attr' attribute.
setattr(obj, 'attr', 'x') Sets attribute 'attr' to 'x'.
delattr(obj, 'attr') Deletes attribute 'attr'.