Python introspection tools
As Python is a dynamic language, it's very flexible and allows you to perform actions on its objects to discover their properties or types.
This is called introspection, and allows you to inspect elements without having too much context about the objects to be inspected. This can be performed at runtime, so it can be used while debugging to discover the attributes and methods of any object.
The main starting point is the type
function. The type
function simply returns the class of an object. For example:
>>> my_object = {'example': True}
>>> type(my_object)
<class 'dict'>
>>> another_object = {'example'}
>>> type(another_object)
<class 'set'>
This can be used to double-check that an object is of the expected type
.
A typical example error is to have a problem because a variable can be either an object or None
. In that case, it's possible...