Python Introspection: Difference between revisions
Line 7: | Line 7: | ||
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. | ||
=<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. | <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>AttributeError</code> exception is thrown. | ||
<syntaxhighlight lang='py'> | |||
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}") | |||
</syntaxhighlight> | |||
=The <tt>inspect</tt> Standard Library Module= | =The <tt>inspect</tt> Standard Library Module= |
Revision as of 19:12, 8 July 2022
Internal
TODO
https://www.geeksforgeeks.org/code-introspection-in-python/
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.
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.
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
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.
Example of how classes in a module can be identified:
import inspect
modules = sys.modules.copy()
module = 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])
Module Introspection
Also see:
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])
Class Instance Introspection
Find Static Methods in a Class
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.
Find classes in a module:
import inspect
cls = ...qualification_method = getattr(ct[1], 'some_static_method')