Type introspection allows the program to examine the structure of a type or object at runtime.
For example, at runtime, it is possible to know if a type X has a specific method/function.
An example in python would be:
class foo(object):
def __init__(self, val):
self.x = val
def bar(self):
return self.x
# dir permite a instrospecção
dir(foo(5))
# resultado
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'bar', 'x']
The function dir
allows you to understand what type foo
contain.
Reflection
Introspection is not the same as Reflection. The reflection allows to change the type or object data at execution time (the metadata), the introspection allows the consultation and analysis of the type information.