Skip to content

Instantly share code, notes, and snippets.

@okapies
Last active October 2, 2021 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okapies/25ec937e3be7cf8f0a150be63e822260 to your computer and use it in GitHub Desktop.
Save okapies/25ec937e3be7cf8f0a150be63e822260 to your computer and use it in GitHub Desktop.
hasattr returns False for an object whose __get__ throws AttributeError.
class A:
pass
class B:
def __get__(self, instance, cls=None):
raise AttributeError()
class C:
a = A()
b = B()
>>> C().a
<A object at 0x7fe55bdce6d0>
>>> hasattr(C(), 'a')
True
>>> C().b
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<console>", line 3, in __get__
AttributeError
>>> hasattr(C(), 'b')
False # without the error!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment