from inspect import getmro _sentinel = object() def _check_instance(obj, attr): try: instance_dict = object.__getattribute__(obj, "__dict__") except AttributeError: pass else: if attr in instance_dict: return instance_dict[attr] return _sentinel def _check_class(klass, attr): for entry in getmro(klass): try: return entry.__dict__[attr] except KeyError: pass return _sentinel def getattr_static(obj, attr, default=_sentinel): """Retrieve attributes without triggering dynamic lookup via the descriptor protocol, __getattr__ or __getattribute__. Note: this function may not be able to retrieve all attributes that getattr can fetch (like dynamically created attributes) and may find attributes that getattr can't (like descriptors that raise AttributeError). It can also return descriptors instead of instance members in some cases. See the documentation for details. """ instance_result = _sentinel if not isinstance(obj, type): instance_result = _check_instance(obj, attr) klass = obj.__class__ else: klass = obj klass_result = _check_class(klass, attr) if instance_result is not _sentinel and klass_result is not _sentinel: if (_check_class(type(klass_result), '__get__') is not _sentinel and _check_class(type(klass_result), '__set__') is not _sentinel): return klass_result if instance_result is not _sentinel: return instance_result if klass_result is not _sentinel: return klass_result if obj is klass: # for types we check the metaclass too for entry in getmro(type(klass)): try: return entry.__dict__[attr] except KeyError: pass if default is not _sentinel: return default raise AttributeError(attr)