Python Programming/Reflection

A Python script can find out about the type, class, attributes and methods of an object. This is referred to as reflection or introspection. See also ../Metaclasses/.

Reflection-enabling functions include type, isinstance, callable, dir and getattr.

Type
The type method enables to find out about the type of an object. The following tests return True:
 * type(3) is int
 * type(3.0) is float
 * type(10**10) is long # Python 2
 * type(1 + 1j) is complex
 * type('Hello') is str
 * type([1, 2]) is list
 * type([1, [2, 'Hello']]) is list
 * type({'city': 'Paris'}) is dict
 * type((1,2)) is tuple
 * type(set) is set
 * type(frozenset) is frozenset
 * type(3).__name__ == "int"
 * type('Hello').__name__ == "str"
 * import types, re, Tkinter # For the following examples
 * type(re) is types.ModuleType
 * type(re.sub) is types.FunctionType
 * type(Tkinter.Frame) is types.ClassType
 * type(Tkinter.Frame).__name__ == "classobj"
 * type(Tkinter.Frame).__name__ == "instance"
 * type(re.compile('myregex')).__name__ == "SRE_Pattern"
 * type(type(3)) is types.TypeType
 * type(re.compile('myregex')).__name__ == "SRE_Pattern"
 * type(type(3)) is types.TypeType

The type function disregards class inheritance: "type(3) is object" yields False while "isinstance(3, object)" yields True.

Links:
 * 2. Built-in Functions # type, python.org
 * 8.15. types — Names for built-in types, python.org

Isinstance
Determines whether an object is an instance of a type or class.

The following tests return True: Note that isinstance provides a weaker condition than a comparison using.
 * isinstance(3, int)
 * isinstance([1, 2], list)
 * isinstance(3, object)
 * isinstance([1, 2], object)
 * import Tkinter; isinstance(Tkinter.Frame, Tkinter.Frame)
 * import Tkinter; Tkinter.Frame.__class__.__name__ == "Frame"

Function isinstance and a user-defined class:

Links:
 * Built-in Functions # isinstance, python.org
 * isinstance considered harmful, canonical.org

Issubclass
Determines whether a class is a subclass of another class. Pertains to classes, not their instances.

Links:
 * Built-in Functions # issubclass, python.org

Duck typing
Duck typing provides an indirect means of reflection. It is a technique consisting in using an object as if it was of the requested type, while catching exceptions resulting from the object not supporting some of the features of the class or type.

Links:
 * Glossary # duck-typing, python.org

Callable
For an object, determines whether it can be called. A class can be made callable by providing a __call__ method.

Examples:
 * callable(2)
 * Returns False. Ditto for callable("Hello") and callable([1, 2]).
 * callable([1,2].pop)
 * Returns True, as pop without "" returns a function object.
 * callable([1,2].pop)
 * Returns False, as [1,2].pop returns 2 rather than a function object.

Links:
 * Built-in Functions # callable, python.org

Dir
Returns the list of names of attributes of an object, which includes methods. Is somewhat heuristic and possibly incomplete, as per python.org.

Examples:
 * dir(3)
 * dir("Hello")
 * dir([1, 2])
 * import re; dir(re)
 * Lists names of functions and other objects available in the re module for regular expressions.

Links:
 * Built-in Functions # dir, python.org

Getattr
Returns the value of an attribute of an object, given the attribute name passed as a string.

An example: The list of attributes of an object can be obtained using.
 * getattr(3, "imag")

Links:
 * Built-in Functions # getattr, python.org

Keywords
A list of Python keywords can be obtained from Python: Links:
 * 32.6. keyword — Testing for Python keywords, python.org

Built-ins
A list of Python built-in objects and functions can be obtained from Python:

Links:
 * 28.3. __builtin__ — Built-in objects, python.org
 * Built-in Functions # dir, python.org