Python 3.11+inspectfunction
inspect.getmembers_static()
members and modules
Like getmembers(), but without invoking descriptors, __getattr__ or __getattribute__: it lists attributes without running the object's code.
Syntax
inspect.getmembers_static(object, predicate=None) -> list[tuple[str, object]]
The values here are raw: for a property you get the property object itself, not the getter's result, and for a method the function from the class, not a bound method. Attributes that the object makes up on the fly in __getattr__ do not show up. In exchange the walk is safe for proxies, lazy ORM models and objects whose properties raise, where getmembers() would run extra code or fail.
Examples
import inspect
class Model:
def __init__(self):
self.id = 1
@property
def broken(self):
raise RuntimeError('нет соединения')
pairs = [(n, type(v).__name__) for n, v in inspect.getmembers_static(Model())
if not n.startswith('_')]
print(pairs) # → [('broken', 'property'), ('id', 'int')]
try:
inspect.getmembers(Model())
except RuntimeError as e:
print('getmembers упал:', e) # → getmembers упал: нет соединения
See also
Open in the full glossarydocs.python.orgReport an inaccuracy