Introspection or reflection is the ability of software to identify and report its own internal structures, such as types, variable scope, methods and attributes.
Native interpreter functions for introspection:
Function | Returns |
---|---|
type(object) |
The typo (class) of the object |
id(object) |
object identifier |
locals() |
local variables dictionary |
globals() |
global variables dictionary |
vars(object) |
object symbols dictionary |
len(object) |
size of an object |
dir(object) |
A list of object structures |
help(object) |
Object doc strings |
repr(object) |
Object representation |
isinstance(object, class) |
True if object is derived from class |
issubclass(subclass, class) |
True if object inherits the class |
The object identifier is a unique number that is used by the interpreter for identifying the objects internally.
Example:
# Getting some information
# about global objects in the program
from types import ModuleType
def info(n_obj):
# Create a reference to the object
obj = globals()[n_obj]
# Show object information
print 'Name of object:', n_obj
print 'Identifier:', id(obj)
print 'Typo:', type(obj)
print 'Representation:', repr(obj)
# If it is a module
if isinstance(obj, ModuleType):
print 'itens:'
for item in dir(obj):
print item
print
# Showing information
for n_obj in dir()[:10]: # The slice [:10] is used just to limit objects
info(n_obj)
Name of object: In Identifier: 58164072 Typo: <type 'list'> Representation: ['', u"# Getting some information\n# about global objects in the program\n\nfrom types import ModuleType\n\ndef info(n_obj):\n\n # Create a refer\xeance to the object\n obj = globals()[n_obj]\n\n # Show object information \n print 'Name of object:', n_obj\n print 'Identifier:', id(obj)\n print 'Typo:', type(obj)\n print 'Representation:', repr(obj)\n\n # If it is a module\n if isinstance(obj, ModuleType):\n print 'itens:'\n for item in dir(obj):\n print item\n print\n\n# Showing information\nfor n_obj in dir()[:10]: # The slice [:10] is used just to limit objects\n info(n_obj)"] Name of object: ModuleType Identifier: 8686656 Typo: <type 'type'> Representation: <type 'module'> Name of object: Out Identifier: 58493664 Typo: <type 'dict'> Representation: {} Name of object: _ Identifier: 140086544672008 Typo: <type 'str'> Representation: '' Name of object: __ Identifier: 140086544672008 Typo: <type 'str'> Representation: '' Name of object: ___ Identifier: 140086544672008 Typo: <type 'str'> Representation: '' Name of object: __builtin__ Identifier: 140086544653064 Typo: <type 'module'> Representation: <module '__builtin__' (built-in)> itens: ArithmeticError AssertionError AttributeError BaseException BufferError BytesWarning DeprecationWarning EOFError Ellipsis EnvironmentError Exception False FloatingPointError FutureWarning GeneratorExit IOError ImportError ImportWarning IndentationError IndexError KeyError KeyboardInterrupt LookupError MemoryError NameError None NotImplemented NotImplementedError OSError OverflowError PendingDeprecationWarning ReferenceError RuntimeError RuntimeWarning StandardError StopIteration SyntaxError SyntaxWarning SystemError SystemExit TabError True TypeError UnboundLocalError UnicodeDecodeError UnicodeEncodeError UnicodeError UnicodeTranslateError UnicodeWarning UserWarning ValueError Warning ZeroDivisionError __IPYTHON__ __IPYTHON__active __debug__ __doc__ __import__ __name__ __package__ abs all any apply basestring bin bool buffer bytearray bytes callable chr classmethod cmp coerce compile complex copyright credits delattr dict dir divmod dreload enumerate eval execfile file filter float format frozenset get_ipython getattr globals hasattr hash help hex id input int intern isinstance issubclass iter len license list locals long map max memoryview min next object oct open ord pow print property range raw_input reduce reload repr reversed round set setattr slice sorted staticmethod str sum super tuple type unichr unicode vars xrange zip Name of object: __builtins__ Identifier: 140086544653064 Typo: <type 'module'> Representation: <module '__builtin__' (built-in)> itens: ArithmeticError AssertionError AttributeError BaseException BufferError BytesWarning DeprecationWarning EOFError Ellipsis EnvironmentError Exception False FloatingPointError FutureWarning GeneratorExit IOError ImportError ImportWarning IndentationError IndexError KeyError KeyboardInterrupt LookupError MemoryError NameError None NotImplemented NotImplementedError OSError OverflowError PendingDeprecationWarning ReferenceError RuntimeError RuntimeWarning StandardError StopIteration SyntaxError SyntaxWarning SystemError SystemExit TabError True TypeError UnboundLocalError UnicodeDecodeError UnicodeEncodeError UnicodeError UnicodeTranslateError UnicodeWarning UserWarning ValueError Warning ZeroDivisionError __IPYTHON__ __IPYTHON__active __debug__ __doc__ __import__ __name__ __package__ abs all any apply basestring bin bool buffer bytearray bytes callable chr classmethod cmp coerce compile complex copyright credits delattr dict dir divmod dreload enumerate eval execfile file filter float format frozenset get_ipython getattr globals hasattr hash help hex id input int intern isinstance issubclass iter len license list locals long map max memoryview min next object oct open ord pow print property range raw_input reduce reload repr reversed round set setattr slice sorted staticmethod str sum super tuple type unichr unicode vars xrange zip Name of object: __doc__ Identifier: 48549272 Typo: <type 'str'> Representation: 'Automatically created module for IPython interactive environment' Name of object: __name__ Identifier: 140086544389776 Typo: <type 'str'> Representation: '__main__'
Python also has a module called types, which has the definitions of the basic types of the interpreter.
Example:
import types
s = ''
if isinstance(s, types.StringType):
print 's is a string.'
s is a string.
Through introspection, it is possible to determine the fields of a database table, for example.
The module inspect provides a set of high-level functions that allow for introspection to investigate types, collection items, classes, functions, source code and the runtime stack of the interpreter.
Example:
import os.path
# inspect: "friendly" introspection module
import inspect
print 'Object:', inspect.getmodule(os.path)
print 'Class?', inspect.isclass(str)
# Lists all functions that exist in "os.path"
print 'Member:',
for name, struct in inspect.getmembers(os.path):
if inspect.isfunction(struct):
print name,
Object: <module 'posixpath' from '/home/csig/env/teste/lib/python2.7/posixpath.pyc'> Class? True Member: _joinrealpath abspath basename commonprefix dirname exists expanduser expandvars getatime getctime getmtime getsize isabs isdir isfile islink ismount join lexists normcase normpath realpath relpath samefile sameopenfile samestat split splitdrive splitext walk
The functions that work with the stack of the interpreter should be used with caution because it is possible to create cyclic references (a variable that points to the stack item that has the variable itself). The existence of references to stack items slows the destruction of the items by the garbage collector of the interpreter.