Python 3.2+inspectfunction
inspect.getattr_static()
static attribute lookup
Looks up an attribute without running code: properties, __getattr__ and __getattribute__ are not invoked. Meant for tools that only inspect an object.
Syntax
inspect.getattr_static(obj, attr[, default])
Plain getattr, and even hasattr, executes a property with all its side effects. getattr_static returns what sits in the dictionaries: the property object rather than its value, a function rather than a bound method, a staticmethod rather than a function. Attributes produced by __getattr__ are invisible to it: you get AttributeError or the default.
Examples
import inspect
class P:
@property
def val(self):
print('property executed')
return 42
def __getattr__(self, name):
return 'dyn:' + name
p = P()
print(getattr(p, 'val')) # → property executed / 42
print(type(inspect.getattr_static(p, 'val')).__name__) # → property
print(getattr(p, 'zzz'), inspect.getattr_static(p, 'zzz', 'нет')) # → dyn:zzz нет
import inspect
class S:
@staticmethod
def sm(): pass
def m(self): pass
print(type(inspect.getattr_static(S, 'sm')).__name__, type(S.sm).__name__)
# → staticmethod function
print(type(inspect.getattr_static(S(), 'm')).__name__, type(S().m).__name__)
# → function method
See also
Open in the full glossarydocs.python.orgReport an inaccuracy