Python Introspection: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 12: Line 12:
=Overview=
=Overview=
Introspection is Python's equivalent for Java reflection. It is the ability to determine the type of an object at runtime and to dynamically examine Python objects.
Introspection is Python's equivalent for Java reflection. It is the ability to determine the type of an object at runtime and to dynamically examine Python objects.
=Introspect the Members of an Object Instance=
All members of an object instance can be identified using <code>inspect.getmembers()</code>.
=<tt>getattr()</tt>=
=<tt>getattr()</tt>=
<code>[[Python Language Functions#getattr|getattr()]]</code> is a built-in function that returns the value of the specified attribute from the specified object. If an attribute with such name does not exist, an <code>[[Python Language Exceptions#AttributeError |AttributeError]]</code> exception is thrown.
<code>[[Python Language Functions#getattr|getattr()]]</code> is a built-in function that returns the value of the specified attribute from the specified object. If an attribute with such name does not exist, an <code>[[Python Language Exceptions#AttributeError |AttributeError]]</code> exception is thrown.

Revision as of 02:49, 4 January 2023

Internal

TODO

https://www.geeksforgeeks.org/code-introspection-in-python/


Module Internal Representation and Introspection

Module Internal Representation and Introspection

Overview

Introspection is Python's equivalent for Java reflection. It is the ability to determine the type of an object at runtime and to dynamically examine Python objects.

Introspect the Members of an Object Instance

All members of an object instance can be identified using inspect.getmembers().

getattr()

getattr() is a built-in function that returns the value of the specified attribute from the specified object. If an attribute with such name does not exist, an AttributeError exception is thrown.

Class Introspection with getattr()

A specific static method can be identified by querying the class attributes with inspect.getmembers() and selecting the matching method, or by name with getattr() builtin.

cls = ...
method_name = 'some_static_method'
try:
  method = getattr(cls, method_name)
  print(method)
except AttributeError:
  print(f"{method_name} method not found in class {cls}")

The inspect Standard Library Module

https://docs.python.org/3/library/inspect.html#module-inspect

inspect is a Standard Library module.

getmembers()

getmembers() returns all members of an object as (name, value) pairs sorted by name. The object can be a module, class, etc. A predicate can be provided to the function, and the function returns only return members that satisfy a given predicate.

Module Introspection with getmembers()

Also see:

Python Language Modularization

Find classes in a module:

import inspect

module = sys.modules['some_package.some_subpackage.SomeClass']
class_tuples = inspect.getmembers(module, inspect.isclass)
for ct in class_tuples:
  print('class name: ', ct[0])
  print('class: ', ct[1])

Invoking Functions and Methods Dynamically

Dynamic Invocation