Being a powerful dynamic language, Python allows us to change its runtime behavior based on the state of objects it's working with.
Inspecting the state of objects is the foundation of every dynamic language, and the standard library inspect
module has most of the features needed for such a case.
For this recipe, the following steps are to be performed:
- Based on the
inspect
module, we can quickly create a helper function that will tell us major object properties and type for most objects:
import inspect def inspect_object(o): if inspect.isfunction(o) or inspect.ismethod(o): print('FUNCTION, arguments:', inspect.signature(o)) elif inspect.isclass(o): print('CLASS, methods:', inspect.getmembers(o, inspect.isfunction)) else: print('OBJECT ({}): {}'.format( o.__class__, [(n, v) for n, v in inspect.getmembers(o) if not n.startswith('__')] ))
- Then, if we apply it to any object...