Introspection (inspect)

say it: “in-truh-SPEK-shun” /ˌɪntrəˈspɛkʃən/ or “in-SPEKT” /ɪnˈspɛkt/

A program looking at itself while it runs: types, attributes, function arguments.

What is introspection?

Introspection is a program looking at itself while it runs: what type something is, what attributes it has, what arguments a function takes. The inspect module helps.

import inspect

def book_room(city, nights=1):
    pass

print(inspect.signature(book_room))  # (city, nights=1)
print(hasattr("hello", "upper"))      # True

Level: Master · More Python terms at this level

Updated