Skip to main content

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 signature
  • inspect.getsource(obj) → get source code
  • inspect.getmembers(obj) → get members and attributes

Wrap-Up

inspect allows advanced runtime analysis of objects, functions, and classes.