introspection

A program examining objects and classes at run time, for example with hasattr, getattr, type and isinstance.

In more detail

Introspection lets code ask an object about itself while it runs. hasattr(obj, name) checks the object and then its class, while hasattr(Class, name) sees only what the class defines, never attributes created on self in __init__. getattr reads an attribute by name with an optional default, and setattr creates or changes one.

Example

class R:
    def __init__(self):
        self.battery = 90

print(hasattr(R(), 'battery'), hasattr(R, 'battery'))   # True False

Related terms

Taught in

References

Certification courses