Last active
October 2, 2021 10:14
-
-
Save okapies/25ec937e3be7cf8f0a150be63e822260 to your computer and use it in GitHub Desktop.
hasattr returns False for an object whose __get__ throws AttributeError.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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