7. Reflection with Inspect
The inspect module provides detailed reflection on objects.
Example
import inspect
def foo(a, b=1):
return a + b
print(inspect.signature(foo))
print(inspect.getsource(foo))
Useful inspect Functions
inspect.signature(func)→ get function signatureinspect.getsource(obj)→ get source codeinspect.getmembers(obj)→ get members and attributes
Wrap-Up
inspect allows advanced runtime analysis of objects, functions, and classes.