from inspect import getmro _sentinel = object() 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 reported by dir(obj) and can return descriptors instead of instance members in some cases. See the documentation for details. """ try: instance_dict = object.__getattribute__(obj, "__dict__") except AttributeError: # C types may not have a __dict__ or a __slots__ slots = getattr(obj, '__slots__', _sentinel) if slots is not _sentinel and attr in slots: try: # attempt to fetch the slot, we could use type(obj) # here, which would bypass __class__ proxying, but # we use obj.__class__ elsewhere *anyway* and # better to have consistent behaviour for objects # that lie about __class__ desc = getattr_static(obj.__class__, attr) except AttributeError: # this code path is only possible if __slots__ has # been tampered with, harmless anyway pass else: try: return desc.__get__(obj) except AttributeError: # arguably incorrect - as the instance member is # not set on this object return desc else: if attr in instance_dict: return instance_dict[attr] if not isinstance(obj, type): klass = obj.__class__ else: klass = obj for entry in getmro(klass): try: return entry.__dict__[attr] except KeyError: pass 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)